从文本文件中提取变量的值
Extract the value of a variable from a text file
以下是文本文件的片段 (txt.toml):
value = 10.0
base = 14.0
outcome = 20.0
numbers = [12.0, 20.0]
input = false
Scheme = "default"
sigma = [1, 8, 11, 5]
我想访问变量“base”的值。我尝试了 中的以下解决方案:
variable = {}
with open('txt.toml', 'r') as file:
for line in file:
name, value = line.replace(' ', '').strip('=')
variable[name] = value
print(variable['base'])
抛出以下错误:
ValueError: too many values to unpack (expected 2)
我无法修复此错误。如何访问存储在变量“base”中的值?
您可以使用 str.partition() to extract the name
and the value
part separated by an "="
. Then you can use str.strip() 删除空格。
variable = {}
with open('txt.toml', 'r') as file:
for line in file:
name, _, value = line.partition('=')
variable[name.strip()] = value.strip()
print(variable['base'])
$ python3 src.py
14.0
即使您的源数据中有空格,即使您的值中有多个等号 "="
,此解决方案也有效
txt.toml
...
Scheme = "default value here = (12 == 12.0)"
...
代码
...
print(variable['Scheme'])
输出
$ python3 src.py
"default value here = (12 == 12.0)"
使用 split
而不是 strip
。
试试这个:
variable = {}
with open('txt.toml', 'r') as file:
for line in file:
name, value = line.replace(' ', '').split('=')
variable[name] = value
variable
输出:
{'value': '10.0\n',
'base': '14.0\n',
'outcome': '20.0\n',
'numbers': '[12.0,20.0]\n',
'input': 'false\n',
'Scheme': '"default"\n',
'sigma': '[1,8,11,5]'}
您需要 split
字符串而不是 strip
字符串。 split
returns 一个列表和 strip()
returns 一个字符串,其中从字符串的任一侧删除了所需的子字符串。
.split
将根据分隔符将字符串拆分为列表,.strip()
从字符串的开头和开头剥离字符串。
variable = {}
with open('txt.toml', 'r') as file:
for line in file:
name, value = line.replace(' ', '').split('=')
variable[name] = value
print(variable['base'])
with open("txt.toml") as fd:
lines = fd.read().splitlines()
lines = [line.split("=") for line in lines]
result = {i[0].strip() : i[1].strip() for i in lines}
print(result)
print(result['base'])
以下是文本文件的片段 (txt.toml):
value = 10.0
base = 14.0
outcome = 20.0
numbers = [12.0, 20.0]
input = false
Scheme = "default"
sigma = [1, 8, 11, 5]
我想访问变量“base”的值。我尝试了
variable = {}
with open('txt.toml', 'r') as file:
for line in file:
name, value = line.replace(' ', '').strip('=')
variable[name] = value
print(variable['base'])
抛出以下错误:
ValueError: too many values to unpack (expected 2)
我无法修复此错误。如何访问存储在变量“base”中的值?
您可以使用 str.partition() to extract the name
and the value
part separated by an "="
. Then you can use str.strip() 删除空格。
variable = {}
with open('txt.toml', 'r') as file:
for line in file:
name, _, value = line.partition('=')
variable[name.strip()] = value.strip()
print(variable['base'])
$ python3 src.py
14.0
即使您的源数据中有空格,即使您的值中有多个等号 "="
,此解决方案也有效
txt.toml
...
Scheme = "default value here = (12 == 12.0)"
...
代码
...
print(variable['Scheme'])
输出
$ python3 src.py
"default value here = (12 == 12.0)"
使用 split
而不是 strip
。
试试这个:
variable = {}
with open('txt.toml', 'r') as file:
for line in file:
name, value = line.replace(' ', '').split('=')
variable[name] = value
variable
输出:
{'value': '10.0\n',
'base': '14.0\n',
'outcome': '20.0\n',
'numbers': '[12.0,20.0]\n',
'input': 'false\n',
'Scheme': '"default"\n',
'sigma': '[1,8,11,5]'}
您需要 split
字符串而不是 strip
字符串。 split
returns 一个列表和 strip()
returns 一个字符串,其中从字符串的任一侧删除了所需的子字符串。
.split
将根据分隔符将字符串拆分为列表,.strip()
从字符串的开头和开头剥离字符串。
variable = {}
with open('txt.toml', 'r') as file:
for line in file:
name, value = line.replace(' ', '').split('=')
variable[name] = value
print(variable['base'])
with open("txt.toml") as fd:
lines = fd.read().splitlines()
lines = [line.split("=") for line in lines]
result = {i[0].strip() : i[1].strip() for i in lines}
print(result)
print(result['base'])