[英]Reading multiline variables from a text file in Python
我正在將文本文件讀入python。 文本文件包含(大量)變量,變量名稱在左側以字符串形式給出,在右側以等號 (=) 分隔值。 例如
Proc_Method = 2
Obs_Method = 4
只要變量的值在一行中給出,我就可以正確讀出變量的值:
namevalue = data.split('=')
name = namevalue[0].strip()
value = namevalue[1].strip()
但是,如果變量分布在多行(即數組)上。 此代碼僅將數組的第一行分配給變量,然后再轉到下一個變量。 因此,如果我有以下形式的變量:
Corr_Mat = 10 0 0
20 10 0
0 20 10
上面的代碼將聲明該值等於 10 0 0,然后轉到下一個變量。 有沒有一種方法可以定義值,以便它采用以等號開頭的所有行,並在文本文件中下一個相等之前的行結束?
使用這樣的文件:
Proc_Method = 2
Obs_Method = 4
Corr_Mat = 10 0 0
20 10 0
0 20 10
Proc_Method = 2
您可以遵循類似堆棧的方法,將新變量作為鍵放入字典variables
中,並將所有以下行作為值附加到鍵中。
variables = {}
with open('file.txt') as f:
for line in f:
if '=' in line:
# add a new key-value pair to the dictionary,
# remember the current variable name
current_variable, value = line.split('=')
variables[current_variable] = value
else:
# it's a continued line -> add to the remembered key
variables[current_variable] += line
結果:
{'Proc_Method ': ' 2\n',
'Obs_Method ': ' 4\n',
'Corr_Mat ': ' 10 0 0 \n 20 10 0\n 0 20 10\n'}
或者,您可以讀取整個文件並使用正則表達式來提取變量。
該模式搜索行的開頭(前導^
與re.MULTILINE
組合),后跟任意數量的符號( .+
)后跟=
后跟任意數量的不等號符號( [^=]+
) 后跟換行符。
import re
with open('file.txt') as f:
file_content = f.read()
chunks = re.findall(r'^.+=[^=]+\n', file_content, flags=re.MULTILINE)
結果:
['Proc_Method = 2\n',
'Obs_Method = 4\n',
'Corr_Mat = 10 0 0 \n 20 10 0\n 0 20 10\n',
'Proc_Method = 2\n']
當然,這需要一些清理,並且只有在變量不包含任何可能無法保證的=
s 時才有效。
試試這個代碼。
params = {}
last_param = ''
for line in data.splitlines():
line = line.strip()
if '=' in line:
sp = line.split('=')
last_param = sp[0]
params[last_param] = sp[1]
else:
params[last_param] += f'\n{line}'
print(params)
結果:
{'Proc_Method ': ' 2', 'Obs_Method ': ' 4', 'Corr_Mat ': ' 10 0 0\n20 10 0\n0 20 10'}
我得到了一個更清晰的結果,但可以說是更混亂的代碼。 但我想有很多簡單的方法可以讓這段代碼更干凈......
文件輸入:
Proc_Method = 2
Obs_Method = 4
Corr_Mat = 10 0 0
20 10 0
0 20 10
test = 4
test2 = 4
woof = a b c
s sdfasd sdas
sda as a
代碼:
output = {}
with open ("E:\Coding\stackoverflow\input.txt", "r") as file:
lines = file.readlines()
previous_full_line = 0
for line_number, line in enumerate(lines):
line_content = line.strip()
if "=" in line_content:
namevalue = line_content.split('=')
name = namevalue[0].strip()
values = namevalue[1].strip()
values = values.split(" ")
line_list = []
for value in values:
line_list.append(value)
output[name] = line_list
previous_full_line = line_number
else:
values = line_content.split(" ")
new_list = []
for value in values:
new_list.append(value)
output[name].extend(new_list)
print(output)
結果
{
"Proc_Method":[
"2"
],
"Obs_Method":[
"4"
],
"Corr_Mat":[
"10",
"0",
"0",
"20",
"10",
"0",
"0",
"20",
"10"
],
"test":[
"4"
],
"test2":[
"4"
],
"woof":[
"a",
"b",
"c",
"s",
"sdfasd",
"sdas",
"sda",
"as",
"a"
]
}
從這個輸出結果我猜你可以隨意使用數據。 知道字典“輸出”中的所有屬性和“鍵”都在一個數組中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.