python - 将文件中的字符串分组到字典中

标签 python python-3.x dictionary

我正在尝试将列表中的字符串分组到字典中。我读入一个文件以获取字符串列表。我想获取该列表并按 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/

相关文章:

python - Python 中 Lambda 表达式与本地函数的速度测试

python-3.x - 在 QGraphicsView 中禁用鼠标指针

c# - 检查字典中是否存在某个项目并将其从 C# 中的字典中删除

python - 数据帧放入 bin 中

python - Google App Engine - Python - 任务队列 - 如何添加任务列表?

python - scrapy Spider 的输出与 scrapy shell 的输出不同

python - 尝试运行服务器时出现导入错误

python: ValueError: 加载字典结构时字符串格式错误

java - 如何使用 for 循环保存映射中的整数

python - 如何使用 Python 在 Windows 上创建带有嵌入式斜线的文件?