给出了一些代码(我使用的是 Python 3.2),并不断收到以下错误。
import csv
import collections
import itertools
grid = collections.Counter()
with open("test1.csv", "r") as fp:
reader = csv.reader(fp)
for line in reader:
for pair in itertools.combinations(line, 2):
grid[pair] += 1
grid[pair[::-1]] += 1
actors = sorted(set(pair[0] for pair in grid))
with open("connection_grid.csv", "wb") as csvfile:
writer = csv.writer(fp)
writer.writerow([''] + actors)
for actor in actors:
line = [actor,] + [grid[actor, other] for other in actors]
writer.writerow(line)
但我收到了这个错误。
回溯(最后一次调用):文件“C:/Python32/test.py”,第 21 行,在 writer.writerow([''] +actors) 中 ValueError:对已关闭文件的 I/O 操作。