我如何从 python 中的 txt 文件制作字典
How can i make a dictionary from txt file in python
我有这个 txt 文件
A:{A:0, B:6, C:4, D:3, E:0, F:0, G:0}
B:{A:6, B:0, C:2, D:0, E:4, F:0, G:0}
C:{A:4, B:2, C:0, D:2, E:0, F:8, G:0}
D:{A:3, B:0, C:2, D:0, E:3, F:0, G:0}
E:{A:0, B:4, C:0, D:3, E:0, F:7, G:6}
F:{A:0, B:0, C:8, D:0, E:7, F:0, G:6}
G:{A:0, B:0, C:0, D:0, E:6, F:6, G:0}
titles = []
with open("graph.txt", "r") as file:
for line in file:
column=line.split(":")
title=column[0]
titles.append(title)
我需要为我得到的每个标题制作字典,例如 A、B、C
正确格式化每一行,你可以使用ast.literal_eval
。我使用正则表达式查找每个键并将其替换为用引号括起来的相同键。
import ast
import re
KEY_PATTERN = re.compile(r'(\w+?):')
dics = []
with open('graph.txt') as f:
for line in f:
line = line.strip()
if line:
dic_str = "{" + KEY_PATTERN.sub(r'"\g<1>":', line) + "}"
dics.append(ast.literal_eval(dic_str))
print(dics)
可以更短(虽然更难阅读):
import ast
import re
KEY_PATTERN = re.compile(r'(\w+?):')
with open('graph.txt') as f:
dics = [ast.literal_eval("{" + KEY_PATTERN.sub(r'"\g<1>":', line) + "}") for line in f if line.strip()]
print(dics)
输出:
[{'A': {'A': 0, 'B': 6, 'C': 4, 'D': 3, 'E': 0, 'F': 0, 'G': 0}}, {'B': {'A': 6, 'B': 0, 'C': 2, 'D': 0, 'E': 4, 'F': 0, 'G': 0}}, {'C': {'A': 4, 'B': 2, 'C': 0, 'D': 2, 'E': 0, 'F': 8, 'G': 0}}, {'D': {'A': 3, 'B': 0, 'C': 2, 'D': 0, 'E':3, 'F': 0, 'G': 0}}, {'E': {'A': 0, 'B': 4, 'C': 0, 'D': 3, 'E': 0, 'F': 7, 'G': 6}}, {'F': {'A': 0, 'B': 0, 'C': 8, 'D': 0, 'E': 7, 'F': 0, 'G': 6}}, {'G': {'A': 0, 'B': 0, 'C': 0, 'D': 0, 'E': 6, 'F': 6, 'G': 0}}]
如果您希望结果只有一个 dict
而不是更改:
dics = []
# and
dics.append(ast.literal_eval(dic_str))
至
dics = {}
# and
dics.update(ast.literal_eval(dic_str))
这可以通过以下示例实现:
import re
titles = []
with open("graph.txt", "r") as file:
for line in file:
if ':' in line:
title=re.match(r"^(.*?):", line).groups()[0]
dict_str= re.match("^.*?\{(.*?)\}", line).groups()[0]
dictionary = {key:value for (key,value) in (item.strip().split(':') for item in dict_str.split(','))}
titles.append({title: dictionary})
for item in titles:
print(item)
这将产生如下输出:
{'A': {'A': '0', 'C': '4', 'B': '6', 'E': '0', 'D': '3', 'G': '0', 'F': '0'}}
{'B': {'A': '6', 'C': '2', 'B': '0', 'E': '4', 'D': '0', 'G': '0', 'F': '0'}}
{'C': {'A': '4', 'C': '0', 'B': '2', 'E': '0', 'D': '2', 'G': '0', 'F': '8'}}
{'D': {'A': '3', 'C': '2', 'B': '0', 'E': '3', 'D': '0', 'G': '0', 'F': '0'}}
{'E': {'A': '0', 'C': '0', 'B': '4', 'E': '0', 'D': '3', 'G': '6', 'F': '7'}}
{'F': {'A': '0', 'C': '8', 'B': '0', 'E': '7', 'D': '0', 'G': '6', 'F': '0'}}
{'G': {'A': '0', 'C': '0', 'B': '0', 'E': '6', 'D': '0', 'G': '0', 'F': '6'}}
我有这个 txt 文件
A:{A:0, B:6, C:4, D:3, E:0, F:0, G:0}
B:{A:6, B:0, C:2, D:0, E:4, F:0, G:0}
C:{A:4, B:2, C:0, D:2, E:0, F:8, G:0}
D:{A:3, B:0, C:2, D:0, E:3, F:0, G:0}
E:{A:0, B:4, C:0, D:3, E:0, F:7, G:6}
F:{A:0, B:0, C:8, D:0, E:7, F:0, G:6}
G:{A:0, B:0, C:0, D:0, E:6, F:6, G:0}
titles = []
with open("graph.txt", "r") as file:
for line in file:
column=line.split(":")
title=column[0]
titles.append(title)
我需要为我得到的每个标题制作字典,例如 A、B、C
正确格式化每一行,你可以使用ast.literal_eval
。我使用正则表达式查找每个键并将其替换为用引号括起来的相同键。
import ast
import re
KEY_PATTERN = re.compile(r'(\w+?):')
dics = []
with open('graph.txt') as f:
for line in f:
line = line.strip()
if line:
dic_str = "{" + KEY_PATTERN.sub(r'"\g<1>":', line) + "}"
dics.append(ast.literal_eval(dic_str))
print(dics)
可以更短(虽然更难阅读):
import ast
import re
KEY_PATTERN = re.compile(r'(\w+?):')
with open('graph.txt') as f:
dics = [ast.literal_eval("{" + KEY_PATTERN.sub(r'"\g<1>":', line) + "}") for line in f if line.strip()]
print(dics)
输出:
[{'A': {'A': 0, 'B': 6, 'C': 4, 'D': 3, 'E': 0, 'F': 0, 'G': 0}}, {'B': {'A': 6, 'B': 0, 'C': 2, 'D': 0, 'E': 4, 'F': 0, 'G': 0}}, {'C': {'A': 4, 'B': 2, 'C': 0, 'D': 2, 'E': 0, 'F': 8, 'G': 0}}, {'D': {'A': 3, 'B': 0, 'C': 2, 'D': 0, 'E':3, 'F': 0, 'G': 0}}, {'E': {'A': 0, 'B': 4, 'C': 0, 'D': 3, 'E': 0, 'F': 7, 'G': 6}}, {'F': {'A': 0, 'B': 0, 'C': 8, 'D': 0, 'E': 7, 'F': 0, 'G': 6}}, {'G': {'A': 0, 'B': 0, 'C': 0, 'D': 0, 'E': 6, 'F': 6, 'G': 0}}]
如果您希望结果只有一个 dict
而不是更改:
dics = []
# and
dics.append(ast.literal_eval(dic_str))
至
dics = {}
# and
dics.update(ast.literal_eval(dic_str))
这可以通过以下示例实现:
import re
titles = []
with open("graph.txt", "r") as file:
for line in file:
if ':' in line:
title=re.match(r"^(.*?):", line).groups()[0]
dict_str= re.match("^.*?\{(.*?)\}", line).groups()[0]
dictionary = {key:value for (key,value) in (item.strip().split(':') for item in dict_str.split(','))}
titles.append({title: dictionary})
for item in titles:
print(item)
这将产生如下输出:
{'A': {'A': '0', 'C': '4', 'B': '6', 'E': '0', 'D': '3', 'G': '0', 'F': '0'}}
{'B': {'A': '6', 'C': '2', 'B': '0', 'E': '4', 'D': '0', 'G': '0', 'F': '0'}}
{'C': {'A': '4', 'C': '0', 'B': '2', 'E': '0', 'D': '2', 'G': '0', 'F': '8'}}
{'D': {'A': '3', 'C': '2', 'B': '0', 'E': '3', 'D': '0', 'G': '0', 'F': '0'}}
{'E': {'A': '0', 'C': '0', 'B': '4', 'E': '0', 'D': '3', 'G': '6', 'F': '7'}}
{'F': {'A': '0', 'C': '8', 'B': '0', 'E': '7', 'D': '0', 'G': '6', 'F': '0'}}
{'G': {'A': '0', 'C': '0', 'B': '0', 'E': '6', 'D': '0', 'G': '0', 'F': '6'}}