DBF导入Charmap错误Python

时间:2017-01-28 01:14:40

标签: python python-2.7 decoding dbf

关于导入我还有另一个问题,但我遇到了另一个问题。我试图从DBF文件导入数据,虽然大多数DBF文件都有效,但我碰到了一个给我以下错误的文件,

"C:\Program Files\Anaconda2\python.exe" D:/Projects/DBFImport/DBFImporter/extractdbf.py
Traceback (most recent call last):
  File "D:/Projects/DBFImport/DBFImporter/extractdbf.py", line 17, in <module>
for record in table.records:
  File "C:\Program Files\Anaconda2\lib\site-packages\dbfread\dbf.py", line 316, in _iter_records
for field in self.fields]
  File "C:\Program Files\Anaconda2\lib\site-packages\dbfread\field_parser.py", line 79, in parse
return func(field, data)
  File "C:\Program Files\Anaconda2\lib\site-packages\dbfread\field_parser.py", line 157, in parseM
return self.decode_text(memo)
  File "C:\Program Files\Anaconda2\lib\site-packages\dbfread\field_parser.py", line 45, in decode_text
return decode_text(text, self.encoding, errors=self.char_decode_errors)
  File "C:\Program Files\Anaconda2\lib\encodings\cp1252.py", line 15, in decode
return codecs.charmap_decode(input,errors,decoding_table)
UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 278: character maps to <undefined>

这里是代码,它非常简单易于分析,

import pyodbc, os, string
from dbfread import DBF

# SQL Server Connection Test
cnxn = pyodbc.connect('DRIVER={SQLServer};SERVER=***********;DATABASE=TEST_DBFIMPORT;UID=test;PWD=test')
cursor = cnxn.cursor()

table = DBF('E:\\Backups\\imp.dbf', lowernames=True)
for record in table.records:
    rec1 = record['id']
    cursor.execute ("insert into imp(ID) values(?)", rec1)
cnxn.commit()

我尝试了各种解码,但似乎没有任何效果。

UPDATE1:

<type 'tuple'>: (<type 'exceptions.UnicodeDecodeError'>, UnicodeDecodeError('charmap', 'Firearms as appraised on May 18, 2011. F.I.E (Firearms Import Export Co.) .26 automatic pistol S/N # AS21212 ----------------- $175.00 Walther (Smith & Wesson) P22, 22LR semi automatic pistol S/N # N052010 -------------- $325.00 Taurus .357 Magnum Model 608 revolver, blue,, 4\xe2\x80\x9d vent rib barrel S/N # LF632765 ------------ $375.00 Colt MKII Series 70 semi automatic pistol, 9mm, blue, pacmeyer grips, S/N # 70S49671 -------- $475.00 Ruger Model 10/22 semi-automatic carbine, 22LR, S/N # 126-90774 ----- $200.00', 278, 279, 'character maps to <undefined>'), None)

1 个答案:

答案 0 :(得分:1)

你收到错误是因为有一些没有unicode映射的代码点(我认为是三个) - 它们只是空白。

使用我的dbf库,您通常会将文件打开为:

table = dbf.Table('e:/Backups/imp.dbf')  # forward slash and backslash both work

您可以通过打印表来查看表本身指定的文件编码:

print table

要覆盖表格中指定的编码:

table = dbf.Table('e:/Backups/imp.dbf', codepage='...')

如果没有其他工作,你可以尝试使用'utf8'代码页 - 它不是dbf规范的一部分,但可能有帮助(我添加它供我自己使用,所以没有任何保证/保证等)。