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')  
for tag, count in cnt.items():  
    fp.write('{}|{}\n'.format(tag, count))  


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='|')  
    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?



I think that, for an easy task like this, the first version is better as it is considerably simpler while accomplishing the same goal.

It can still be improved though:

Cleaner deletion of file extension

infile[:-4] is probably used to remove the extension from the file, but it is not obvious, I suggest infile.replace(".txt","")( where the extension may be different from txt but you get the idea).

Context managing

It is so easy to forget to close a file, it is better to use with that will close it automatically every time:

with open(outfile, encoding='utf-8-sig', mode='w') as fp:
for tag, count in cnt.items():  
    fp.write('{}|{}\n'.format(tag, count))  


cnt is a mysterious name, I have no idea what kind of data it may contain, I suggest coming up with a better name.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.