2

I have a row:

row = {'A':'valueA', 'B':'valueB', 'C':'valueC'}

Basically I want to open a new csv file and write each value in its column, e.g.

 ---------------------------
|   A     |  B     |   C    |
 ---------------------------
|  ValueA | ValueB | ValueC |
 ---------------------------

I am doing this:

def OandWtonew(filename, row):
with open('Ouput1.csv', 'wt') as csvfile:
    fileout = csv.writer(csvfile, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL)
    fileout.writerow(row)

but the output I am getting is:

 --------------------------------------------
|   A           |  B           |  C          |
 --------------------------------------------
|  'A':'valueA' | 'B':'valueB' | 'C':'valueC'|
 --------------------------------------------

All replies are much appreciated :)

5
  • Why aren't you using a DictWriter? Commented Aug 22, 2018 at 18:04
  • @coldspeed Off topic, I know but 100k !! congrats. And I see you found a job at Google (weren't you looking for one?) Commented Aug 22, 2018 at 18:05
  • Looks like you should use csv.dictwriter and not csv.writer Commented Aug 22, 2018 at 18:07
  • 1
    I don't see how you are getting that output with that code. Commented Aug 22, 2018 at 18:07
  • @JoeIddon Thanks! Also, yes, an internship. Months ago, in fact. Actually, I've finished my internship, so I need to update that title. Thanks for the reminder! Commented Aug 22, 2018 at 18:08

3 Answers 3

3

Since the data is a dictionary, csv.DictWriter() may be the most straightforward way to do it. Just make sure the list of header values are in the original order, in this case sorted() may be the easiest:

with open('Ouput1.csv', 'wt') as csvfile:
    fieldnames = sorted(row.keys())  # needs to be the first line in correct order
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
    writer.writeheader()
    writer.writerow(row)             # writes corresponding value for key in header

CSV view in Excel

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

Comments

2

You can use pandas.DataFrame.to_csv() to easily write to a csv:

import pandas as pd

row = {'A':'valueA', 'B':'valueB', 'C':'valueC'}

#Convert dictionary to a pandas dataframe
df = pd.DataFrame().append(row, ignore_index=True)

# check output
df
    A       B       C
0   valueA  valueB  valueC

# Save to a csv file
df.to_csv('Ouput1.csv',index=False)

Comments

1

row is of type dict. From the documentation:

import csv
with open('names.csv', 'w') as csvfile:
    fieldnames = ['first_name', 'last_name']
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

    writer.writeheader()
    writer.writerow({'first_name': 'Baked', 'last_name': 'Beans'})
    writer.writerow({'first_name': 'Lovely', 'last_name': 'Spam'})
    writer.writerow({'first_name': 'Wonderful', 'last_name': 'Spam'})

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.