我有这样一个文件:
1 abc
1 def
2 ghi
3 jkl
3 mno
3 pqr
想从中生成这样的文件:
abc;def
jkl;mno
jkl;pqr
mno;pqr
我有以下代码:
with open('input.txt') as f1:
with open("output.csv", "wb") as f2:
cw = csv.writer(f2, delimiter=";")
for l in itertools.groupby((l.split() for l in f1), lambda x: x[0]):
grouped = set(x[1] for x in l[1]) # set avoids duplicate rows
for c in itertools.combinations(grouped, 2):
cw.writerow(c)
但是这段代码不向输出文件写入任何内容。我做错什么了?你知道吗
使用csv.writer文件在两个字符串之间插入
;
实在是太过分了。去掉它,代码就可以工作了。同样,为了开发和发布,使用iterable of line代替外部文件作为源,使用print
代替write
作为输出。你知道吗印刷品
相关问题 更多 >
编程相关推荐