DBF - encoding cp1250

前端 未结 3 1299
青春惊慌失措
青春惊慌失措 2021-01-18 12:46

I have dbf database encoded in cp1250 and I am reading this database using folowing code:

import csv
from dbfpy import dbf
import os
import sys

filename = s         


        
相关标签:
3条回答
  • 2021-01-18 13:01

    I wrote simpledbf. The line that is causing you problems was from some testing I was doing when developing the module. First of all, you might want to update your installation, as 0.2.6 is the most recent. Then you can try removing that particular line (#557) from the file "D:\ProgramFiles\Anaconda\lib\site-packages\simpledbf\simpledbf.py". If that doesn't work, you can ping me at the GitHub repo for simpledbf, or you could try Ethan's suggestion for the dbf module.

    0 讨论(0)
  • 2021-01-18 13:09

    You can decode and encode as necessary. dbfpy assumes strings are utf8 encoded, so you can decode as it isn't that encoding and then encode again with the right encoding.

    import csv
    from dbfpy import dbf
    import os
    import sys
    
    filename = sys.argv[1]
    if filename.endswith('.dbf'):
        print "Converting %s to csv" % filename
        csv_fn = filename[:-4]+ ".csv"
        with open(csv_fn,'wb') as csvfile:
            in_db = dbf.Dbf(filename)
            out_csv = csv.writer(csvfile)
            names = []
            for field in in_db.header.fields:
                names.append(field.name)
            #out_csv.writerow(names)
            for rec in in_db:
                row = [i.decode('utf8').encode('cp1250') if isinstance(i, str) else i for i in rec.fieldData]
                out_csv.writerow(rec.fieldData)
            in_db.close()
            print "Done..."
    else:
      print "Filename does not end with .dbf"
    
    0 讨论(0)
  • 2021-01-18 13:22

    Try using my dbf library:

    import dbf
    with dbf.Table('test.dbf') as table:
        dbf.export(table, 'junk.csv')
    
    0 讨论(0)
提交回复
热议问题