0

我有5节课,

即:

earn

acq

money

fx

crude

我有一个大约 20000 个文件的列表,并且我有一个文档“topics.txt”,格式如下:

earn~6~7~4

grain~9~1~2~12

money~4~29

依此类推......其中数字对应于文件名,单词对应于类。

我需要打印仅属于我之前提到的类的所有文件,即;“赚”、“acq”、“钱”、“fx”和“原油”

前输出:(earn-6.txt,7.txt,4.txt)

(acq-5.txt)

等等..

我可以打印“topics.txt”中所有可用的类,但我只想打印 5 个特定的类。

import collections
import sys
sys.stdout=open('dicti1.txt','w')
with open('topics.txt') as f:
    d = collections.defaultdict(list)
    for line in f:
        value, *keys = line.strip().split('~')
        for key in filter(None, keys):
            d[key].append(value+".txt")


for i in d.items():
    print(i)    
4

1 回答 1

0

除非我误解了这个问题,否则你正在努力工作。另外我建议不要覆盖sys.stdout.

尝试这样的事情:

interesting_types = ['earn', 'acq', 'money', 'fx', 'crude']
with open("in.txt") as in_file, open('out.txt', 'w') as out_file:
    for l in in_file:
        if l:
            type, *filenames = l.strip().split("~")
            if type in interesting_types:
                out_file.write("({}-{})\n".format(type, ",".join(["{}.txt".format(x) for x in filenames])))
于 2013-06-29T16:11:49.683 回答