0

I concatenated some daily data files of ammonia concentration values and I wanted to sort them in increasing order of latitude and longitude using the help of this answer: sort csv by column

After that, when I see my sortedlist (used in the solution for multiple column sorting) it gives me:

print(sortedlist)
[OrderedDict([('', '7'), ('lat', '10.25'), ('lon', '72.25'), ('nh', '6150295245484870.0'), ('err', '28.55')]), OrderedDict([('', '23'), ('lat', '10.25'), ('lon', '72.25'), ('nh', '6609984397284026.0'), ('err', '42.33')]), OrderedDict([('', '7'), ('lat', '10.25'), ('lon', '72.25'), ('nh', '6150295245484870.0'), ('err', '12.24')]), OrderedDict([('', '23'), ('lat', '10.25'), ('lon', '72.25'), ('nh', '6609984397284026.0'), ('err', '10.34')])]

How do I write these values to another csv file? The values in OrderedDict shown here are very small I have like 500Kb data!!!

1 Answers1

0

The easiest way is to use the csv module:

import csv

my_data = (your data)   # Pass the data you want to have in the csv file here

my_file = open('csv_example_file.csv', 'w')
with my_file:
    writer = csv.writer(my_file)
    writer.writerows(my_data)

print("write Complete")

So to walk you through what we done here, you first have your dict of data. We then use the writer() function to create an object that is suitable to for writing. To iterate over each of the rows, we use the writerows() function.

EDIT: Here's a link that goes into more detail with reading and writing csv files, since it would be too long to leave in here: https://code.tutsplus.com/tutorials/how-to-read-and-write-csv-files-in-python--cms-29907

Connor J
  • 430
  • 3
  • 15