Each data is in 1 row and 1 column. I want to separate them in csv file
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)
I don't know why data are not separte in each row
csv
docs stipulates you should provide newline=''
when working with files, observe how opening is done incsv.DictWriter
example
with open('names.csv', 'w', newline='') as csvfile:
this does apply also to csv.writer
and csv.reader
and allow code to work correctly indepedent from used ends of line.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.