从文本文件中提取变量

huangapple go评论102阅读模式
英文:

Extract variables from text file

问题

抱歉,我只会为您提供翻译的部分,不会回答翻译之外的问题。

"Hi I am reading lines from a file which has the details of variables that I need to further run my program. The code I am using to read the lines from the file is as follows:"
你好,我正在从一个文件中读取包含我需要进一步运行程序的变量详情的行。我使用以下代码从文件中读取这些行:

"file_name="<file path>/inputPS05.txt""
"file=open(file_name)"
"lines = [line.strip(" \n ''") for line in open(file_name)]"
"Please refer the screenshot below:"
请参考下面的截图:

"lines[0] gives a string output which is expected but I want to convert them into variables. How can I achieve that?"
lines[0] 输出一个字符串,这是预期的,但我想将它们转换成变量。我如何实现这个?

"I am pulling index items from the list but I am unable to change it to variables and values."
我正在从列表中提取索引项,但我无法将它们更改为变量和值。

英文:

Hi I am reading lines from a file which has the details of variables that I need to further run my program. The code I am using to read the lines from the file is as follows:

file_name=&quot;&lt;file path&gt;/inputPS05.txt&quot;
file=open(file_name)
lines = [line.strip(&quot; \n &#39;&#39;&quot;) for line in open(file_name)]

Please refer the screenshot below:
从文本文件中提取变量

lines[0] gives a string output which is expected but I want to convert them into variables. How can I achieve that?

I am pulling index items from the list but I am unable to change it to variables and values.

答案1

得分: 1

你可以使用 exec 关键字,尽管出于安全原因不建议使用

for line in lines:
    exec(line)

更建议解析该行并填充一个字典以供参考。

英文:

You can use the exec keyword, although it is not recommended for security reasons.

for line in lines:
    exec(line)

It is rather recommended to parse the line and fill a dict of values for example.

答案2

得分: 1

Using exec will work, but is generally unsafe, as you don't always know what code you might be running. If it's your code, and you know everything in the file, then you can do this:

使用exec会起作用,但通常是不安全的,因为您不总是知道可能运行的代码。如果这是您的代码,并且您知道文件中的所有内容,那么您可以这样做:

For line in lines:
对于行中的每一行:

exec(line)
exec(line)


Further issues with your code:

您的代码还存在进一步问题:

You are setting the file variable, but then not using it in your loop.

您设置了file变量,但然后在循环中未使用它。

Your code should read:

您的代码应该如下:

Lines = [line.strip(" \n '''") for line in file]
Lines = [line.strip(" \n '''") for line in file]

Even better, you should use a context manager:

更好的做法是使用上下文管理器

With open(file_name) as file:
使用open(file_name)作为文件:

Lines = [line.strip(" \n '''") for line in file]
Lines = [line.strip(" \n '''") for line in file]

This will handle closing of the file for you.
这将为您处理文件的关闭。

英文:

Using exec will work, but is generally unsafe, as you don't always know what code you might be running. If it's your code, and you know everything in the file, then you can do this:

for line in lines:
    exec(line)

Further issues with your code:

You are setting the file variable, but then not using it in your loop.

Your code should read:

lines = [line.strip(&quot; \n &#39;&#39;&quot;) for line in file]

Even better, you should use a context manager:

with open(file_name) as file:
    lines = [line.strip(&quot; \n &#39;&#39;&quot;) for line in file]

This will handle closing of the file for you.

huangapple
  • 本文由 发表于 2023年7月6日 16:33:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76626955.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定