dataScienceGuy
dataScienceGuy

Reputation: 25

Trouble editing a CSV file using python csv module dictwriter

I am making a program that reads data from a form, stores it into a dictionary, and then uses csv.DictWrite to make append the data to a csv file. I run the program but nothing happens to my data.csv file. The main program and the data file are in the same working directory, and csvmodule is installed as well. Here's the code,

def response_to_csv(data):
#append w/ dictionary -> more efficiewn
with open('data.csv', 'a', newline = '') as csvfile:
    fieldnames = ['date', 'first', 'last', 'age', 'email', 'country',
             'city/town', 'Uni Student', 'Instagram','Followers','Affiliate'
    ]
    writer = csv.DictWriter(csvfile, fieldnames = fieldnames)
    writer.writeheader()
    writer.writerow({
        'date' : data['date'],
        'first': data['first'],
        'last' : data['last'],
        'age'  : data['age'],
        'email': data['email'],
      'country': data['country'],
    'city/town': data['city/town'],
  'Uni Student': data['Uni Student'],
    'Instagram': data['Instagram'],
    'Followers': data['Followers'],
    'Affiliate': data['Affiliate']
    })

Here's the data dictionary

data = {
    'date' : date,
    'first': fname,
    'last' : lname,
    'age'  : age,
    'email': email,
    'country': country,
    'city/town': city_town,
    'Uni Student': is_Uni_Student,
    'Instagram': insta,
    'Followers': ig_followers,
    'Affiliate': affiliation
    }
response_to_csv(data)

Upvotes: 0

Views: 66

Answers (2)

Sam
Sam

Reputation: 1415

Your code worked for me, although I had to fix the indentation of the body of your function, with open(...) should not be at the same indent as def response_to_csv(data):

import csv
def response_to_csv(data):
    #append w/ dictionary -> more efficiewn
    with open('data.csv', 'a', newline = '') as csvfile:
        fieldnames = ['date', 'first', 'last', 'age', 'email', 'country',
                 'city/town', 'Uni Student', 'Instagram','Followers','Affiliate'
        ]
        writer = csv.DictWriter(csvfile, fieldnames = fieldnames)
        writer.writeheader()
        writer.writerow({
            'date' : data['date'],
            'first': data['first'],
            'last' : data['last'],
            'age'  : data['age'],
            'email': data['email'],
          'country': data['country'],
        'city/town': data['city/town'],
      'Uni Student': data['Uni Student'],
        'Instagram': data['Instagram'],
        'Followers': data['Followers'],
        'Affiliate': data['Affiliate']
        })

data = {
    'date' : '2019_01_01',
    'first': 'firstname',
    'last' : 'lname',
    'age'  : '99',
    'email': '[email protected]',
    'country': 'USA',
    'city/town': 'MyTown',
    'Uni Student': True,
    'Instagram': 'MyInsta',
    'Followers': 24,
    'Affiliate': 'affiliation'
}
response_to_csv(data)

$ cat data.csv
date,first,last,age,email,country,city/town,Uni Student,Instagram,Followers,Affiliate
2019_01_01,firstname,lname,99,[email protected],USA,MyTown,True,MyInsta,24,affiliation

Upvotes: 1

Chris
Chris

Reputation: 16172

import csv
data = {
'date' : '202001',
'first': 'Bob',
'last' : 'Smith',
'age'  : 45,
'email': '[email protected]',
'country': 'USA',
'city/town': 'New York',
'Uni Student': 1,
'Instagram': '@bsmith',
'Followers': 45678,
'Affiliate': 'Red Bull'
    }

def response_to_csv(data):
    fieldnames = ['date', 'first', 'last', 'age', 'email', 'country',
                 'city/town', 'Uni Student', 'Instagram','Followers','Affiliate'
        ]
    with open('data.csv', 'a', newline = '') as csvfile:
        writer = csv.DictWriter(csvfile, fieldnames = fieldnames)
        writer.writeheader()
        writer.writerow(data)

response_to_csv(data)

Upvotes: 1

Related Questions