Sam
Sam

Reputation: 1229

Using a Dictionary to write data in columns using Python

Data in test.csv contains only country and dept. This gets filled in at writer.writerow(data).

I've added extra columns using: writer = csv.DictWriter then writer.writeheader().

I'm trying to fill in the columns added using data in a dictionary dicts. The data will fill up to about 200 rows in that column.

import json
import csv

def parse_file(input):
    with open(input, 'r') as input:
        content = input.readlines()
        csv_file = open('test.csv', 'wb+')

        writer = csv.DictWriter(csv_file, fieldnames=["employee_id", "firstname", "lastname", "country", "dept"], delimiter=',')
        writer.writeheader()

        dicts = [{'employee_id':'123456', 'firstname':'Tom', 'lastname':'jones' }]

        for line in content:
            line_segments = line.split('\t')

            data = json.loads(line_segments[0])

            writer.writerow(data)

            for d in dicts:
                print(d['id'])
                writer.writerow([d['employee_id'],d['firstname'],d['lastname']])

Error:

Traceback (most recent call last):
  File "parse_file.py", line 36, in <module>
    parse_input_file(args.input_file)
  File "parse_file.py", line 25, in parse_input_file
    writer.writerow([d['femployee_id'],d['firstname'],d['lastname']])
  File "C:\Users\\AppData\Local\Continuum\Miniconda3\csv.py", line 152, in writerow
    return self.writer.writerow(self._dict_to_list(rowdict))
  File "C:\Users\\AppData\Local\Continuum\Miniconda3\\lib\csv.py", line 148, in _dict_to_list
    + ", ".join([repr(x) for x in wrong_fields]))
ValueError: dict contains fields not in fieldnames: '123456', 'Tom', 'Jones'

Upvotes: 0

Views: 136

Answers (1)

Prakhar Trivedi
Prakhar Trivedi

Reputation: 8526

As the error in the traceback states:

ValueError: dict contains fields not in fieldnames:

The dictionary that comes from the query contains more key than the field names you specified in the DictWriter constructor.

in csv.DictWriter ,you have specified fieldnames as :

fieldnames=["employee_id", "firstname", "lastname", "country", "dept"]

But you are passing only three dict values in this line :

for d in dicts:
    print(d['id'])
    writer.writerow([d['employee_id'],d['firstname'],d['lastname']])

What you need is updation of dictionary named dicts, Like this :

dicts = {'employee_id':'123456', 'firstname':'Tom', 'lastname':'jones','country':'some_country_name', 'dept':'some_dept' }

The final updated code should look something like this :

import json
import csv

def parse_file(input):
    with open(input, 'r') as input:
        content = input.readlines()
        csv_file = open('test.csv', 'wb+')

        writer = csv.DictWriter(csv_file, fieldnames=["employee_id", "firstname", "lastname", "country", "dept"], delimiter=',')
        writer.writeheader()

        dicts = {'employee_id':'123456', 'firstname':'Tom', 'lastname':'jones','country':'some_country_name', 'dept':'some_dept' }

        for line in content:
            line_segments = line.split('\t')
            data = json.loads(line_segments[0])
            writer.writerow(data)
            writer.writerow(dicts)

Upvotes: 1

Related Questions