0

I've got a long list of numbers in a one-column csv file, and I am running the following code (the backbone of which was kindly offered by @jpp) to generate a ranked list of the most frequent occurrences in the list:

import csv
from collections import Counter
from itertools import chain
from io import StringIO
import numpy

with open('origList.csv', 'r') as infile:
    # define lazy reader object
    reader = csv.reader(open('origList.csv', 'r'))
    # flatten, convert to int, feed to Counter object
    c = Counter(map(int, chain.from_iterable(reader)))

# calculate 2000 most common items, return number and counts
print('\n'.c.most_common(2000))
numpy.savetxt("topRankers.csv", c.most_common(2000), delimiter=",")

But the cv output file (TopRankers.csv) has the actual numbers written in scientific notation instead of decimal and I need the latter (example of the first couple rows below). Any ideas on how I can make this happen? Many thanks in advance.

2.204781298301920000e+08,1.700000000000000000e+01
4.641279012832990000e+09,1.700000000000000000e+01
8.001912089290390840e+17,1.500000000000000000e+01
user2047228
  • 73
  • 1
  • 3
  • 10
  • @david-z this doesn't seem to be a duplicate question to me. the code i've shown above is totally different from the two answers you linked, and i don't have any idea how to implement the solutions in those threads to solve my problem. could someone help with a more specific answer regarding my question? thanks. – user2047228 Jul 20 '18 at 03:31
  • 1
    Hm, well you're right that it's not exact, but I was thinking that you're basically asking how to format the output fields when using `numpy.savetxt`, which is broadly the same thing that those other questions are asking about. In particular the first one seems quite closely related, and you should be able to get a sense of how to use `fmt` from that. If you're still having trouble, could you edit your question to show what you tried from the other thread and how it didn't work for you? – David Z Jul 20 '18 at 03:35
  • Hey @david-z, appreciate the quick reply. I tried changing that last line in my code above by adding in the fmt command like so: numpy.savetxt("topRankers.csv", c.most_common(2000), delimiter=",", fmt='% 4d') And I got this error: AttributeError: 'str' object has no attribute 'c' – user2047228 Jul 20 '18 at 03:46
  • 1
    Ah, well that's an entirely different question ;-) I think that one's just a typo, though (otherwise I would suggest posting it separately). Look where you have `'\n'.c` in the call to `print` on the next-to-last line. – David Z Jul 20 '18 at 03:48
  • That's solved it! Dumb error on my part. Thanks @david-z! – user2047228 Jul 20 '18 at 03:52

0 Answers0