0

My goal is to write a series of lists to a csv, where each list is a column.

This is my code so far:

rows = list(zip(columns['phase'],columns['start_date']))

with open('date_update.csv','w') as f:
    writer = csv.writer(f)
    for row in rows:
        writer.writerows(row);

This is the csv it produces. Both lists are the same length. When I print rows it has an extra '),' at the beginning. But this is not present when you print the two lists (columns['phase'] and columns['start_date']).

Is there something wrong with the zip line? Or am I not writing it to the csv file correctly?

1
  • Please don't post images. Help guidelines prefer text as it is searchable and cut-n-pastable. Commented Feb 21, 2019 at 2:01

3 Answers 3

1

You can use a pd.DataFrame and then save it to a csv. For example:

import pandas as pd
columns = {}

columns['phase'] = ['Phase 1','Phase 2', 'Phase 3','Phase 4']
columns['start_date'] = ['August 24 September','August 24 September',
                         'August 24 September', 'August 24 September']

data = list(zip(columns['phase'],columns['start_date']))

df = pd.DataFrame(data = data)

df.to_csv('date_update.csv', index=False, header=False)

Will save in the file 'date_update.csv' the following data:

Phase 1,August 24 September
Phase 2,August 24 September
Phase 3,August 24 September
Phase 4,August 24 September

Sign up to request clarification or add additional context in comments.

Comments

0

Either use:

for row in rows:
    writer.writerow(row)

Or:

writer.writerows(rows)

Note the use of singular and plural forms. writerows requires a sequence of sequences. A string is a sequence, so passing a list of strings makes it use a string as a sequence of columns and list each character in a separate column.

Also per the csv docs, use either of the following to open the file passed to csv.writer:

with open('date_update.csv','wb') as f:           # Python 2
with open('date_update.csv','w',newline='') as f: # Python 3

Full example:

#!python3
import csv

columns = {}
columns['phase']      = ['Phase 1','Phase 2', 'Phase 3','Phase 4']
columns['start_date'] = ['Aug 24, 2018','Sep 25, 2018','Jan 01, 2019','Feb 15, 2019']

rows = zip(columns['phase'],columns['start_date'])

with open('date_update.csv','w',newline='') as f:
    writer = csv.writer(f)
    writer.writerows(rows)

Output:

Phase 1,"Aug 24, 2018"
Phase 2,"Sep 25, 2018"
Phase 3,"Jan 01, 2019"
Phase 4,"Feb 15, 2019"

Comments

0

As an addendum on what @mark-tolonen wrote you could also use DictWriter from the builtin csv module.

with open('date_update.csv','a') as f:
    writr = csv.DictWriter(f, fieldnames=["phase","start_date"])
    # writr.write_header() # Use only if file is created by operation or is empty
    writr.writerows([{"phase": c["phase"], "start_date": c["start_date"]} for c in columns])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.