Saving zip list to csv in Python
Asked Answered
A

3

8

How I can write below zip list to csv file in python?

[{'date': '2015/01/01 00:00', 'v': 96.5},
 {'date': '2015/01/01 00:01', 'v': 97.0},
 {'date': '2015/01/01 00:02', 'v': 93.75},
 {'date': '2015/01/01 00:03', 'v': 96.0},
 {'date': '2015/01/01 00:04', 'v': 94.5}

I have this error:

_csv.Error: sequence expected

My code is here:

import csv
res = zip_list
csvfile = "/home/stm/PycharmProjects/isbak_trafik/example.csv"

with open(csvfile, "w") as output:
    writer = csv.writer(output, lineterminator='\n')
    writer.writerows(res)
Amphibolite answered 8/2, 2016 at 8:26 Comment(1)
The dictionaries are not sequences. Use csv.DictWriter. Or write dict.values()Seismoscope
G
1

writer.writerows expects a sequence of values for writing a single row into the CSV file.

Using your original code:

import csv
res =[{'date': '2015/01/01 00:00', 'v': 96.5}, {'date': '2015/01/01 00:01', 'v': 97.0}, {'date': '2015/01/01 00:02', 'v': 93.75}, {'date': '2015/01/01 00:03', 'v': 96.0}, {'date': '2015/01/01 00:04', 'v': 94.5}]
csvfile = "example.csv"
with open(csvfile, "w") as output:
  writer = csv.writer(output, lineterminator='\n')
  for line in res:
    date = line['date']
    value = line['v']
    writer.writerow([date, value])
Gegenschein answered 8/2, 2016 at 9:0 Comment(0)
D
2

Since I find csv.DictWriter not transparent in what its doing, I would recommend doing the following:

with open(csvfile, "w") as output:
    output.write(';'.join(list(res[0].keys()))+"\n")
    [output.write(';'.join(list(map(str, r.values())))+"\n") for r in res]
Dehart answered 8/2, 2016 at 8:50 Comment(4)
The last line of the code example is just a trick, so that you can write it in one line. It should not be forgotten that this is not recommended by the PEP8 code style guide.Dehart
Thank you. But as a result, I have just "date" and "v" in my csv. However, I want to take these numeric values to my csv file. Please can you help me ? @GerhardHagerer [{'date': '2015/01/01 00:00', 'v': 96.5}, {'date': '2015/01/01 00:01', 'v': 97.0}, {'date': '2015/01/01 00:02', 'v': 93.75}Amphibolite
Sorry, I corrected the code example. I tested it and it works fine in my Python3 shellDehart
Thank you for your help! @GerhardHagererAmphibolite
I
2

You can switch to using Python's DictWriter for this. You can pass a list of column headers, this ensures that the order of the columns in the output is what you require. Only columns in this list are written to the your output file:

import csv

zip_list = [
    {'date': '2015/01/01 00:00', 'v': 96.5},
    {'date': '2015/01/01 00:01', 'v': 97.0},
    {'date': '2015/01/01 00:02', 'v': 93.75},
    {'date': '2015/01/01 00:03', 'v': 96.0},
    {'date': '2015/01/01 00:04', 'v': 94.5}]

csvfile = "/home/stm/PycharmProjects/isbak_trafik/example.csv"

with open(csvfile, "wb") as output:
    writer = csv.DictWriter(output, fieldnames=['date', 'v'])
    writer.writeheader()
    writer.writerows(zip_list)

This would produce the following output:

date,v
2015/01/01 00:00,96.5
2015/01/01 00:01,97.0
2015/01/01 00:02,93.75
2015/01/01 00:03,96.0
2015/01/01 00:04,94.5
Impossibly answered 8/2, 2016 at 9:4 Comment(1)
Thank you!! @MartinEvansAmphibolite
G
1

writer.writerows expects a sequence of values for writing a single row into the CSV file.

Using your original code:

import csv
res =[{'date': '2015/01/01 00:00', 'v': 96.5}, {'date': '2015/01/01 00:01', 'v': 97.0}, {'date': '2015/01/01 00:02', 'v': 93.75}, {'date': '2015/01/01 00:03', 'v': 96.0}, {'date': '2015/01/01 00:04', 'v': 94.5}]
csvfile = "example.csv"
with open(csvfile, "w") as output:
  writer = csv.writer(output, lineterminator='\n')
  for line in res:
    date = line['date']
    value = line['v']
    writer.writerow([date, value])
Gegenschein answered 8/2, 2016 at 9:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.