【发布时间】:2016-01-07 16:43:15
【问题描述】:
我将 CSV 文件的两列输出到一个文本文件,并将所有内容打印在文本文件的一行中。像这样:
Product Name Counter({'Descriptions': 1, 'Descriptions':3})
但我希望它像这样打印出来:
Product Name Counter({'Descriptions': 1,
'Descriptions': 3})
这是我目前的代码:
import csv
import sys
from collections import defaultdict
from collections import Counter
data = defaultdict(list)
class dictionary:
with open ('practice.csv', 'rb') as f:
reader = csv.reader(f)
next(reader, None)
for row in reader:
data[row[2]].append(row[3])
text_file = open("test.txt", "w")
data_count = Counter()
for key in data.keys():
sys.stdout = text_file
print key, Counter(data[key])
text_file.close()
我怎样才能让我的文本文件不在一行上打印出来? 不知道如何让它工作。
【问题讨论】:
-
您是否尝试在打印语句的末尾添加换行符 (
'\n')? -
是的,我已经尝试过 ('\n'),但它不起作用。