Python: 文本文件到字典
Python: Text file into dictionary
我想将文本文件按行方式转换为键值对。
我的文本文件
21:54:26 From Rohan luthra : yes
21:54:36 From Ankit : yup
21:54:36 From Ankit : yup
21:55:04 From Rajesh : shubh shubh bolo sir
我想做的是转换成像
这样的键值对
{'Rohan luthra' : 'yes',
'Ankit' : 'yup,}
像这样^
我找不到任何合适的解决方案。
我做了什么
with open(x) as f:
lines = f.readlines()
with open(x, 'r') as f:
for line in f:
splitLine = line.split()
temp_dict[(splitLine[0])] = " ".join(splitLine[2:])
# Dirty hack to remove timestamp
temp_array = temp_dict.values()
chat_dict = dict(s.split(':') for s in temp_array)
pp.pprint(temp_dict)
但是当遇到一行中有两个“:”时,这个方法就失败了。
它returns:
Traceback (most recent call last):
File "filereader.py", line 37, in <module>
most_talkative()
File "filereader.py", line 32, in most_talkative
chat_dict = dict(s.split(':') for s in temp_array)
ValueError: dictionary update sequence element #35 has length 3; 2 is required
with open(x) as f:
lines = f.readlines()
with open(x, 'r') as y:
for line in y:
splitLine = line.split()
temp_dict[(splitLine[0])] = " ".join(splitLine[2:])
# Dirty hack to remove timestamp
temp_array = temp_dict.values()
chat_dict = dict(s.split(':')[:2] for s in temp_array)
这似乎行得通!
@Alan Leuthard 给出的解决方案
循环仅适用于 20 行,而实际文件有 100 多行+
返回 len(lines) 给出实际编号。文件中的行数,即 119.
以下内容根据提供的文件格式的任意长度版本创建字典。请记住,每个键只能有 1 个值,因此重复的键将被后面的值覆盖。
x = 'path\to\file'
temp_dict = dict()
chat_dict = dict()
with open(x, 'r') as f:
for line in f:
splitLine = line.split()
temp_dict[(splitLine[0])] = " ".join(splitLine[2:])
# Dirty hack to remove timestamp
temp_array = temp_dict.values()
chat_dict.update(dict(s.split(':')[:2] for s in temp_array))
print(chat_dict)
我想将文本文件按行方式转换为键值对。 我的文本文件
21:54:26 From Rohan luthra : yes
21:54:36 From Ankit : yup
21:54:36 From Ankit : yup
21:55:04 From Rajesh : shubh shubh bolo sir
我想做的是转换成像
这样的键值对{'Rohan luthra' : 'yes',
'Ankit' : 'yup,}
像这样^ 我找不到任何合适的解决方案。 我做了什么
with open(x) as f:
lines = f.readlines()
with open(x, 'r') as f:
for line in f:
splitLine = line.split()
temp_dict[(splitLine[0])] = " ".join(splitLine[2:])
# Dirty hack to remove timestamp
temp_array = temp_dict.values()
chat_dict = dict(s.split(':') for s in temp_array)
pp.pprint(temp_dict)
但是当遇到一行中有两个“:”时,这个方法就失败了。 它returns:
Traceback (most recent call last):
File "filereader.py", line 37, in <module>
most_talkative()
File "filereader.py", line 32, in most_talkative
chat_dict = dict(s.split(':') for s in temp_array)
ValueError: dictionary update sequence element #35 has length 3; 2 is required
with open(x) as f:
lines = f.readlines()
with open(x, 'r') as y:
for line in y:
splitLine = line.split()
temp_dict[(splitLine[0])] = " ".join(splitLine[2:])
# Dirty hack to remove timestamp
temp_array = temp_dict.values()
chat_dict = dict(s.split(':')[:2] for s in temp_array)
这似乎行得通! @Alan Leuthard 给出的解决方案
循环仅适用于 20 行,而实际文件有 100 多行+
返回 len(lines) 给出实际编号。文件中的行数,即 119.
以下内容根据提供的文件格式的任意长度版本创建字典。请记住,每个键只能有 1 个值,因此重复的键将被后面的值覆盖。
x = 'path\to\file'
temp_dict = dict()
chat_dict = dict()
with open(x, 'r') as f:
for line in f:
splitLine = line.split()
temp_dict[(splitLine[0])] = " ".join(splitLine[2:])
# Dirty hack to remove timestamp
temp_array = temp_dict.values()
chat_dict.update(dict(s.split(':')[:2] for s in temp_array))
print(chat_dict)