I'n trying to convert a dbf file to a csv file(or anything else really). I found this on the interwebs but I get an error

import csv
from dbfpy import dbf
import sys

dbf_fn = 'in.dbf'
csv_fn = 'out.csv'

in_db = dbf.Dbf(dbf_fn)
out_csv = csv.writer(open(csv_fn, 'wb'))

names = []
for field in in_db.header.fields:
    names.append(field.name)
out_csv.writerow(names)

for rec in in_db:
    out_csv.writerow(rec.fieldData)

in_db.close()

error:

Traceback (most recent call last):
  File "piotest.py", line 9, in <module>
    out_csv = csv.writer(open(csv_fn, 'wb'))
AttributeError: 'module' object has no attribute 'writer'

I'm not sure which module this is refering to?

Recommended Answers

All 8 Replies

What does this little test do?

import csv

csv_fn = 'out.csv'
out_csv = csv.writer(open(csv_fn, 'wb'))

print(out_csv)

maybe you have saved your code with name csv.py and you are importing it. Add print csv after import to check the path of file imported.

Thanks for the suggestions

import csv
csv_fn = 'out.csv'
out_csv = csv.writer(open(csv_fn, 'wb'))

results in:

Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'module' object has no attribute 'writer'

and

>>> print csv <module 'csv' from '/home/myfolder/.python-eggs/csv-1.0-py2.6-solaris-2.10-i86pc.egg-tmp/csv.so'>

I should probably mention that all I want to do is extract the infotmation from the dbf file. Getting csv working would be nice but is at this stage a secondary concern. Sorry I didn't make this clearer.

Does your information contain semicolons, you could just write ';'.join(str(data) for data in in_db) to file. Data fields rarely use semicolon, so that is my favorite separator for unquoted fields csv.

wow:

>>> list = [str(data) for data in in_db]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "build/bdist.solaris-2.10-i86pc/egg/dbfpy/dbf.py", line 242, in __getitem__
File "build/bdist.solaris-2.10-i86pc/egg/dbfpy/record.py", line 121, in fromStream
File "build/bdist.solaris-2.10-i86pc/egg/dbfpy/record.py", line 140, in fromString
File "build/bdist.solaris-2.10-i86pc/egg/dbfpy/fields.py", line 173, in decodeFromRecord
File "build/bdist.solaris-2.10-i86pc/egg/dbfpy/fields.py", line 342, in decodeValue
NotImplementedError

Do not name your script "csv.py" ;)

AAARGH!!! That was so silly of me, naming my file csv.py! Thanks!! Nisar

Be a part of the DaniWeb community

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