简体   繁体   中英

How to write dictionary to csv file that already exists and already contains headers?

I'm still kind of new to python, so please excuse my ignorance. I have a CSV file, and it already containers the headers in the first row: column_1 , column_2 , and column_3 . I have a dictionary that maps the columns' names to their respective integers, and I would like to write this dictionary in the CSV file, with the integers appearing in the next row. However, 1 keeps getting appended at the end of column_3 , as in column_31 , and 2 and 3 appear in the two cells right next to column_3 .

bigdict = {'column_1':1,'column_2':2,'column_3':3}

f = path

with open(f,'a+') as csv_file:
    fieldnames = ['column_1','column_2','column_3']
    writer = csv.DictWriter(csv_file, fieldnames=fieldnames,delimiter=',')
    csv_file.readline()
    writer.writerow(bigdict)

I have written text files before, I have read CSV files, and I have also 'opened' non-existing CSV files and written dictionaries to them, but writing dictionaries to existing CSV files is trumping me for some reason.

Above, I did not create the headers, because they already existed. I wrote csv_file.readline() so that the script can read the second row, instead of later writing the dictionary to the first row. When I finally call .writerow , for some reason, it's writing the dictionary values appended to the end of the first row, as described above...

What am I doing wrong here?

I followed Rakesh's method below, and I got the following errors:

Traceback (most recent call last):

  File "<ipython-input-30-a0a3cb0b4ddd>", line 1, in <module>
    runfile('/Users/[name]/Documents/webscraper/tests/dictwritetoexistingcsv.py', wdir='/Users/[name]/Documents/webscraper/tests')

  File "/Users/[name]/anaconda/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 866, in runfile
execfile(filename, namespace)

  File "/Users/[name]/anaconda/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

  File "/Users/[name]/Documents/webscraper/tests/dictwritetoexistingcsv.py", line 13, in <module>
writer.writerow(bigdict)

  File "/Users/[name]/anaconda/lib/python3.6/csv.py", line 155, in writerow
return self.writer.writerow(self._dict_to_list(rowdict))

TypeError: a bytes-like object is required, not 'str'

Hoping I understood your question correctly, I think the problem is that your csv file does not have the newline character at the end of the last row. So, I'll check for that first before going ahead and appending the new row.

I'm starting with this simple csv file named bigcsv.csv which is in the same directory with the script. It's contents initially are:

column_1,column_2,column_3
a, b, c

This is the code:

import os
import csv

dir_path = os.getcwd()
file_path = os.path.join(dir_path, "bigcsv.csv")


bigdict = {'column_1': 1, 'column_2': 2, 'column_3': 3}

with open(file_path, 'a+') as csv_file:
    fieldnames = ['column_1', 'column_2', 'column_3']
    writer = csv.DictWriter(csv_file, fieldnames=fieldnames, delimiter=',')
    if '\n' not in csv_file.readlines()[-1]:
        csv_file.write("\n")
    writer.writerow(bigdict)

This is the file after I run the script.

column_1,column_2,column_3
a, b, c
1,2,3

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM