【问题标题】:Split text file with same value拆分具有相同值的文本文件
【发布时间】:2022-11-12 14:37:40
【问题描述】:

我有一个看起来像这样的文本文件

Apple TreeTwo
Banana TreeOne
Juice TreeOne
Pineapple TreeThree
Berries TreeThree

如何选择具有相同树名的行并将它们放在单独的文件中,如下面的 python

file1.txt
Banana TreeOne
Juice TreeOne

file2.txt
Apple TreeTwo

file3.txt
Pineapple
Berries

我试过使用这个“https://*.com/questions/72065988/how-to-select-all-rows-with-the-same-name-but-different-values-in-python”但没有属性分组错误。我的专栏没有标题,所以不知道这是怎么做还是有其他方法?

f = open('data.txt' , 'r')
f_splits = [v for k, v in f.groupby()]
for f_split in f_splits:
    print(f_split, sep = '\n')

【问题讨论】:

  • 您能否展示您正在尝试的代码,以便我们了解您为什么会收到您提到的错误?
  • 我已经在上面更新了

标签: python


【解决方案1】:

我实际上不会在这里使用 groupby ,只需遍历文件内容然后将其分成列表更容易。

我在下面的示例中使用了字典,因为它能够轻松处理未知值。

data = """Apple TreeTwo
Banana TreeOne
Juice TreeOne
Pineapple TreeThree
Berries TreeThree"""

result = {}
for line in data.splitlines():
    # get the last word to determine which list to put it in
    sort_key = line.split()[-1]
    if sort_key not in result:
        # if the key is not already in the dict, create a new list with 
        # line as the first element
        result[sort_key] = [line]
    else:
        # if the key is already there, append line to the list
        result[sort_key].append(line)

# print it out
for key, value in result.items():
    print(f"{key} → {value}")



# write to files
for key, value in result.items():
    with open(f"{key}.txt", "w") as outfile:
        for line in value:
            outfile.write(f"{line}
")

输出

TreeTwo → ['Apple TreeTwo']
TreeOne → ['Banana TreeOne', 'Juice TreeOne']
TreeThree → ['Pineapple TreeThree', 'Berries TreeThree']

【讨论】: