hey folks,
i'm trying myself on a csv based adressbook but I get the following error:

Traceback (most recent call last):
  File "C:\Dokumente und Einstellungen\Mo\Desktop\adressbook.py", line 11, in <module>
    writer.writerows(data)
  File "C:\Python26\lib\csv.py", line 149, in writerows
    rows.append(self._dict_to_list(rowdict))
  File "C:\Python26\lib\csv.py", line 140, in _dict_to_list
    ", ".join(wrong_fields))
ValueError: dict contains fields not in fieldnames:L,a,s,t,n,a,m,e

This is my code so far:

import csv

reader = csv.DictReader(open('adress.csv', 'rb'))

for row in reader:
    print row

writer = csv.DictWriter(open('adress1.csv', 'wb'), ['Firstname', 'Lastname', 'Phone'])
writer.writerow({'Firstname' : 'Firstname', 'Lastname' : 'Lastname', 'Phone' : 'Phone'})
data = {'Firstname' : 'Carl', 'Lastname' : 'Johnson', 'Phone' : '987651'}
writer.writerows(data)

I don't know what the error message is trying to tell me...
Do you get something out of it?

Thanks in advance.

Recommended Answers

Hello,

For further information, take a look, at this thread ...

http://www.daniweb.com/forums/thread184037.html

Jump to Post

All 2 Replies

Okay - I got it ...

writer.writerows(data)

If you want to add a single row the method is called 'writerow'

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.