Python没有处理所有输入文件相等?

时间:2016-12-09 15:07:09

标签: python csv

我有这段代码:

import csv, itertools

with open("test.csv") as f:
    with open("output.csv","w",newline="") as f2:
    # with open("output.csv","wb") as f2:   # uncomment for python 2 (comment above!)

        cw = csv.writer(f2,delimiter=";")
        for l in itertools.groupby((l.split() for l in f),lambda x : x[0]):
            grouped = set(x[1] for x in l[1])  # set avoids duplicate rows
            if len(grouped)>1:
                for c in itertools.combinations(grouped,2):
                    cw.writerow(c)

它曾用于读取格式为:

的文件
1     abc
1     def
2     ghi
3     jkl
3     mno
3     pqr

并输出为:

abc;def
jkl;mno
jkl;pqr
mno;pqr

但是当我重用代码时它现在不起作用了。使用this input file,它会产生一个空的output.csv。

0 个答案:

没有答案