小编som*_*oma的帖子

如何从Python中读取文件中的参数值

我开始学习Python,并在尝试从文件中读取值时遇到一些问题.

我的参数文件有点像这样:

var1 11111111
path_value "some/space containing path/file.txt"
var3 something
#some other values
var4 some/value1
var5 some/value2
var6 some/value3
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

file=open('this_file.txt')
for line in file:
    fields = line.strip().split()
    if(fields[0] in "var1"):
        ## this will give me 11111111
        var_1_value=fields[1]
    if(fields[0] in "path_value"):
        ## this will give me only till "/some/space
        path_value_contains=fields[1]
Run Code Online (Sandbox Code Playgroud)

如何正确获取路径?我不确定这是一种有效的做事方式.你能告诉我任何更好的方法吗?

我没有使用任何模块.

python file

5
推荐指数
1
解决办法
1289
查看次数

标签 统计

file ×1

python ×1