每个数据是在1行和1列。我想把它们分开的csv文件
from dataclasses import field, fields
import os
import csv
import collections
y=[]
Dict_New=[]
a=['Name','Count']
d={}
path = r"C:\Users\piotr\Desktop\CV"
files = os.listdir(path)
files_file = [f for f in files if os.path.isfile(os.path.join(path, f))]
print(files_file) # ['file1', 'file2.txt', 'file3.jpg']
for x in files_file:
y.append(x[:-4])
print(y)
Counter=collections.Counter(y)
print(Counter)
for key, values in Counter.items():
Dict_New.append({'Name': key, "Count": values})
print(Dict_New)
with open ("test.csv",'w') as f:
write = csv.DictWriter(f,fieldnames=a)
write.writeheader()
write.writerows(Dict_New)
我不知道为什么数据不每行分开
1条答案
按热度按时间uubf1zoe1#
csv
docs规定在使用文件时应提供newline=''
,请观察csv.DictWriter
示例中的打开方式这确实也适用于
csv.writer
和csv.reader
,并且允许代码独立于所使用的行末端正确地工作。