我正在尝试解析下面列出的文本文件。
BIOSReleaseDate = 04/13/2016
BIOSVersionString = 2.1.5
BaseBoardChassisSlot = Slot 08
BatteryRollupStatus = 1
BladeGeometry = 0
这种文本文件大约有40到50行。我能够将此输出读取到文本文件。从那里开始,我一直坚持如何分割LHS值和RHS值。
答案 0 :(得分:1)
尝试.split()方法
with open('file.txt') as f:
lines = f.readlines()
d = {} #optional dictionary to store data
for l in lines:
parts = l.split(' = ') #stores the LHS and RHS of the line in a list
lhs = parts[0]
rhs = parts[1]
d[lhs] = rhs #store data in dictionary
答案 1 :(得分:1)
split()方法可能会派上用场。你需要迭代线。
>>> string = "name=foo"
>>> print string.split("=")
["name", "foo"]