2
\$\begingroup\$

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?

asked Jan 22, 2016 at 21:18
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Simplicity

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

Naming

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

answered Jan 23, 2016 at 11:34
\$\endgroup\$
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.