In Python 3.4, a Counter
object called cnt
like this:
Counter({'0200': 3, '3000': 2, '3011': 2, '0210': 1, '4000': 1})
is to be written to a comma-separated file.
I first tried with CSV and DictWriter
that I had not used before, but I kept getting misty errors and was pressed for time, so I decided to make this simple version (it works):
outfile = infile[:-4] + '_freq.csv'
fp = open(outfile, encoding='utf-8-sig', mode='w')
fp.write('KMC|freq\n')
for tag, count in cnt.items():
fp.write('{}|{}\n'.format(tag, count))
fp.close()
Then when I had some time I felt I should learn to use DictWriter
and CSV, assuming it'd be 'better' or 'more pythonic'.
That turned into this (works too):
outfile2 = infile[:-4] + '_freq2.csv'
with open(outfile2, encoding='utf-8-sig', mode='w', newline='') as f:
# Note: empty newline to avoid blank lines in the output file
fieldnames = ['KMC', 'freq']
writer = csv.DictWriter(f, fieldnames=fieldnames, delimiter='|')
writer.writeheader()
for tag, count in cnt.items():
writer.writerow({'KMC': tag, 'freq': str(count)})
The trick with the empty newline to avoid blank lines is from here.
I like the first approach better. I think CSV and DictWriter
are intended for much larger amounts of columns than the two small ones I got. But I'm no expert.
Any learned comments?