I have a list
of JSON that I print it like this:
for item in points:
print(format(item))
The result looks like this:
{u'TEMP': 30, u'LIGHT': 315, u'HUMIDITY': 30.9, u'SOURCE': u'arduino_1', u'PLACE': u'kitchen', u'time': u'2016-12-31T11:18:38.822822913Z'}
{u'TEMP': 31, u'LIGHT': 325.5, u'HUMIDITY': 31.93, u'SOURCE': u'arduino_1', u'PLACE': u'garage', u'time': u'2016-12-31T11:18:39.919019993Z'}
{u'TEMP': 32, u'LIGHT': 336, u'HUMIDITY': 32.96, u'SOURCE': u'arduino_1', u'PLACE': u'living_room', u'time': u'2016-12-31T11:18:41.014792508Z'}
{u'TEMP': 33, u'LIGHT': 346.5, u'HUMIDITY': 33.99, u'SOURCE': u'arduino_1', u'PLACE': u'basement', u'time': u'2016-12-31T11:18:42.11100167Z'}
First of all, there is a problem with my data source that prints that 'u' characters before every item.
I want to write each line in a CSV file with a format like this ( the first line is the CSV header)
TIME,SOURCE,PLACE,TEMP,LIGHT,HUMIDITY
2016-12-31T11:18:38.822822913Z,arduino_1,kitchen,30,315,30.9
I am trying to do this using the csv
package. But I am not sure how can I get the data of each row out of the items in the list and change the order of where they appear in the resulting CSV file:
with open('output.csv', 'wb') as csvfile:
writer = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
writer.writerow(points)
I would appreciate your help toward a Python noob.
DictWriter
is made for you, writing a list of dicts into a csv file. – Commencementu
then? – Sable