我正在尝试将列表中的字符串分组到字典中。我读入一个文件以获取字符串列表。我想获取该列表并按 ID 对所有项目进行分组。
这就是文件 (logtest.txt) 包含的内容...
Id: 1
FATAL ERROR: Network error: Connection timed out
Done
Return Code: 0
Id: 2
FATAL ERROR: Network error: Connection timed out
Done
Return Code: 0
Id: 3
FATAL ERROR: Network error: Connection timed out
Done
Return Code: 0
到目前为止,我已将文件中的所有行读入列表中。然后我想获取这些字符串并将它们按 id
分组。将数字放入字典中,其中键是 id
数字和值都是 Id: 1
中的所有字符串到下一个包含 Id:
的字符串。
def getAllTheLinesInLogFile():
f = open('logtest.txt', 'r')
return f.readlines()
def getDictOfItems(allLinesInFile):
dict = {}
# ???
# items = allLinesInFile.groupby()
for item in items:
print("{0}".format(item))
return dict
logFile = open('logtest.txt', 'w+')
allLinesInLogFile = getAllTheLinesInLogFile()
dictOfItems = getDictOfItems(allLinesInLogFile)
for item in dictOfItems:
print(item.key)
最佳答案
您可以使用itertools.groupby
对按Id:
分隔的部分进行分组:
from itertools import groupby
with open("in.txt") as f:
d = {}
groups = groupby(f, lambda x: x.startswith("Id:"))
for k, v in groups:
if k: # if we have a line with "Id:.."
# use the line as the key
k = next(v).rstrip()
# call next on the grouper object extracting
# the second item which is our section of lines
d[k] = list(map(str.rstrip, next(groups)[1]))
输入:
Id: 1
FATAL ERROR: Network error: Connection timed out
Done
Return Code: 0
Id: 2
FATAL ERROR: Network error: Connection timed out
Done
Return Code: 0
Id: 3
FATAL ERROR: Network error: Connection timed out
Done
Return Code: 0
输出:
from pprint import pprint as pp
{'Id: 1': ['FATAL ERROR: Network error: Connection timed out',
'Done',
'Return Code: 0'],
'Id: 2': ['FATAL ERROR: Network error: Connection timed out',
'Done',
'Return Code: 0'],
'Id: 3': ['FATAL ERROR: Network error: Connection timed out',
'Done',
'Return Code: 0']}
如果您的数据实际上有多个空行,代码仍然可以工作,如果您不想要空行,您可以过滤它们。如果您想保留换行符,只需删除 str.rstrip
调用即可。
如果您打算在完成一些工作后覆盖该文件,那么随时写入临时文件可能是更好的方法。
关于python - 将文件中的字符串分组到字典中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31224678/