英文:
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="<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?
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(" \n ''") for line in file]
Even better, you should use a context manager:
with open(file_name) as file:
lines = [line.strip(" \n ''") for line in file]
This will handle closing of the file for you.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论