转换为CSV错误(dbfpy)

时间:2017-08-30 17:05:01

标签: csv export-to-csv dbf

我在将DBF转换为CSV

时出错
import csv
from dbfpy import dbf
import os
import sys

filename = 'crop1-fx.dbf'
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:
            out_csv.writerow(rec.fieldData)
        in_db.close()
        print ("Done...")
else:
  print ("Filename does not end with .dbf")
  

“C:\用户\用户\ Anaconda2 \ python.exe”   “C:/Users/User/Desktop/Python/23/dbf/convertCSV.py”   将crop1-fx.dbf转换为csv

Traceback (most recent call last):  

  File "C:/Users/User/Desktop/Python/23/dbf/convertCSV.py", line 17, in 
    for rec in in_db:

  File "C:\Users\User\Anaconda2\lib\site-packages\dbfpy\dbf.py", line 262, in __getitem__
    return self.RecordClass.fromStream(self, self._fixIndex(index))

  File "C:\Users\User\Anaconda2\lib\site-packages\dbfpy\record.py", line 121, in fromStream
    return cls.fromString(dbf, cls.rawFromStream(dbf, index), index) 

  File "C:\Users\User\Anaconda2\lib\site-packages\dbfpy\record.py", line 140, in fromString
    [_fd.decodeFromRecord(string) for _fd in dbf.header.fields]) 

  File "C:\Users\User\Anaconda2\lib\site-packages\dbfpy\fields.py", line 178, in decodeFromRecord
    return self.decodeValue(self.rawFromRecord(record)) 

  File "C:\Users\User\Anaconda2\lib\site-packages\dbfpy\fields.py", line 246, in decodeValue
    return float(value)

  ValueError: invalid literal for float(): -1.#IND00000000000

1 个答案:

答案 0 :(得分:1)

错误正是它所说的:-1.#IND00000000000不是有效的float。因此,要么您的表格已损坏,pdfpy中存在错误(在这种情况下不太可能),或者您的表格仍有24位数字。

顺便提一下,根据最初的dbf规范,24位数字无效。