with open("filename") as infile:
for line in infile.read().splitlines():
if line.startswith("s_alis"):
s_alis = line.split("'")[1]
elif line.startswith("xps_entity"):
xps_entity = line.split("'")[1]
#etc
作为返回词典的函数。
def return_dictionary(filename):
# define empty dict
result = {}
with open(filename) as infile:
for line in infile.read().splitlines():
if line.startswith("s_alis"):
result["s_alis"] = line.split("'")[1]
elif line.startswith("xps_entity"):
result["xps_entity"] = line.split("'")[1]
#etc
return result
list_of_filenames = [
"file1.txt",
"file2.txt"
]
not_sure_what_to_name_this = []
for filename in list_of_filenames:
not_sure_what_to_name_this.append(return_dictionary(filename))
from pprint import pprint
pprint(not_sure_what_to_name_this)
1条答案
按热度按时间byqmnocz1#
一些简单的字符串操作和拆分将完成您想要的操作。我建议阅读
str.split()
、str.startswith()
、通用文件处理和列表处理作为返回词典的函数。