我有一个文件,实际上不是一个xml文件,而是一个文本文件,看起来像这样(example.txt(-
<01-01-20>
hello hello . . random content
</01-01-20>
<04-01-20>
hello again. . some more random content.....
</04-01-20>
我想将文本文件中的值存储为字典中的键值对,类似于:
{<01-01-20>:"hello hello. . ",<04-01-20>:"hello again.. . "}
这可能吗。请指导我如何在python 中做到这一点
编辑-
我想出的代码,
import re
import mmap
tf1 = open('text1.txt', 'r+b') ##first kosha
tf2 = open('text2.txt', 'r') ##second kosha
first = []
second = []
reg = re.compile("^<.*>$") ##sample regular expression for < >
for line in tf1:
first += reg.findall(line)
for line in tf2:
second += reg.findall(line)
print('Tags that are present in file 1 but not in file2')
for i in first:
if i not in second:
print(i)
tf1.close()
tf2.close()
现在我需要比较两个文本文件中的hyms,判断它们是否相似,所以我想最好把它放进字典里。请帮忙。
这是您实际期望的完整代码。
代码
with open("file_in.txt", "r") as file:
dict1 = {}
lines = file.readlines()
for i in range(len(lines)):
try:
if lines[i].startswith('<') and lines[i+1] != 'n':
dict1[lines[i].strip()] = lines[i+1].strip()
except:
print("File read complete!")
print(dict1)
输出
{'<01-01-20>': 'hello hello . . random content', '<04-01-20>': 'hello again. . some more random content.....'}