Saving zip list to csv in Python

2024/10/15 15:20:35

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)
Answer

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 csvzip_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
https://en.xdnf.cn/q/69270.html

Related Q&A

unable to download the pipeline provided by spark-nlp library

i am unable to use the predefined pipeline "recognize_entities_dl" provided by the spark-nlp library i tried installing different versions of pyspark and spark-nlp libraryimport sparknlp from…

Can __setattr__() can be defined in a class with __slots__?

Say I have a class which defines __slots__:class Foo(object):__slots__ = [x]def __init__(self, x=1):self.x = x# will the following work?def __setattr__(self, key, value):if key == x:object.__setattr__…

mysql-connector python IN operator stored as list

I am using mysql-connector with python and have a query like this:SELECT avg(downloadtime) FROM tb_npp where date(date) between %s and %s and host like %s",(s_date,e_date,"%" + dc + &quo…

Pandas: Use iterrows on Dataframe subset

What is the best way to do iterrows with a subset of a DataFrame?Lets take the following simple example:import pandas as pddf = pd.DataFrame({Product: list(AAAABBAA),Quantity: [5,2,5,10,1,5,2,3],Start…

Can I parameterize a pytest fixture with other fixtures?

I have a python test that uses a fixture for credentials (a tuple of userid and password)def test_something(credentials)(userid, password) = credentialsprint("Hello {0}, welcome to my test".f…

fit method in python sklearn

I am asking myself various questions about the fit method in sklearn.Question 1: when I do:from sklearn.decomposition import TruncatedSVD model = TruncatedSVD() svd_1 = model.fit(X1) svd_2 = model.fit(…

Django 1.9 JSONField update behavior

Ive recently updated to Django 1.9 and tried updating some of my model fields to use the built-in JSONField (Im using PostgreSQL 9.4.5). As I was trying to create and update my objects fields, I came a…

Using Tweepy to search for tweets with API 1.1

Ive been trying to get tweepy to search for a sring without success for the past 3 hours. I keep getting replied it should use api 1.1. I thought that was implemented... because I can post with tweepy.…

Retrieving my own data via FaceBook API

I am building a website for a comedy group which uses Facebook as one of their marketing platforms; one of the requirements for the new site is to display all of their Facebook events on a calendar.Cur…

Python -- Optimize system of inequalities

I am working on a program in Python in which a small part involves optimizing a system of equations / inequalities. Ideally, I would have wanted to do as can be done in Modelica, write out the equation…