如何使用Python将格式化文件解析为变量?
我有一个预格式化的文本文件,其中包含一些变量,如下所示:如何使用Python将格式化文件解析为变量?,python,parsing,Python,Parsing,我有一个预格式化的文本文件,其中包含一些变量,如下所示: header one name = "this is my name" last_name = "this is my last name" addr = "somewhere" addr_no = 35 header header two first_var = 1.002E-3 second_var = -2.002E-8 header 如您所见,每个分数都以字符串头开始,后跟范围名称(一、二等
header one
name = "this is my name"
last_name = "this is my last name"
addr = "somewhere"
addr_no = 35
header
header two
first_var = 1.002E-3
second_var = -2.002E-8
header
如您所见,每个分数都以字符串头开始,后跟范围名称(一、二等)
我不知道如何使用Python以编程方式解析这些选项,使它们能够以这种方式访问我的脚本:
one.name = "this is my name"
one.last_name = "this is my last name"
two.first_var = 1.002E-3
有人能给我指一个教程、一个库或文档的某个特定部分来帮助我实现我的目标吗?我会用生成器解析它,在解析文件时生成部分ast.literal\u eval()
负责将值解释为Python文本:
import ast
def load_sections(filename):
with open(filename, 'r') as infile:
for line in infile:
if not line.startswith('header'):
continue # skip to the next line until we find a header
sectionname = line.split(None, 1)[-1].strip()
section = {}
for line in infile:
if line.startswith('header'):
break # end of section
line = line.strip()
key, value = line.split(' = ', 1)
section[key] = ast.literal_eval(value)
yield sectionname, section
循环上述函数以接收(名称,章节)
元组:
for name, section in load_sections(somefilename):
print name, section
对于示例输入数据,这将导致:
>>> for name, section in load_sections('/tmp/example'):
... print name, section
...
one {'last_name': 'this is my last name', 'name': 'this is my name', 'addr_no': 35, 'addr': 'somewhere'}
two {'first_var': 0.001002, 'second_var': -2.002e-08}
Martijn Pieters给出的答案是正确的,因为您已经预先格式化了文件,但是如果您首先能够以不同的方式格式化文件,您将避免许多潜在的错误。如果我是您,我会考虑将文件格式化为JSON(或XML),因为这样您就可以使用python的JSON(或XML)库为您完成这项工作。除非您使用的是非常糟糕的遗留代码或您无法访问的系统,否则您应该能够首先进入吐出文件的代码,并使其提供更好的文件
def get_section(f):
section=[]
for line in f:
section += [ line.strip("\n ") ]
if section[-1] == 'header': break
return section
sections = dict()
with open('input') as f:
while True:
section = get_section(f)
if not section: break
section_dict = dict()
section_dict['sname'] = section[0].split()[1]
for param in section[1:-2]:
k,v = [ x.strip() for x in param.split('=')]
section_dict[k] = v
sections[section_dict['sname']] = section_dict
print sections['one']['name']
您还可以作为属性访问这些部分:
class Section:
def __init__(self, d):
self.__dict__ = d
one = Section(sections['one'])
print one.name
如果这段代码包含任何解释或注释,那就太好了。