【问题标题】:DBF to CSV ConversionDBF 到 CSV 转换
【发布时间】:2016-11-29 09:11:59
【问题描述】:

我整天都在尝试将 dbf 文件转换为 CSV,但似乎无法获得它。我查看了各种选项,但似乎无法找到一种可行的方法。这是我一直在尝试的。

  import arcpy
  import dbf
  from arcpy import env
  import os

    def DBFtoCSV(path):
 '''Convert every DBF table into CSV table. 
'''
env.workspace = path
tablelist = arcpy.ListTables() # list tables in file
for table in tablelist: # iterate through every table
    #make sure you are just working with .dbf tables 
    if table.endswith('.dbf'):
        with dbf.Table(os.path.join(path, table)) as current_table:
            print current_table
            dbf.export(current_table)
    print "\n Processing ",table[:-4]+".csv table complete."
  if __name__ == '__main__':  
     path=r'path'
DBFtoCSV(path)

我现在得到的错误是:

       Processing  name.csv table complete.

    Table:         F:/name.dbf
    Type:          Visual Foxpro
    Codepage:      cp1252 (Windows ANSI)
    Status:        read-write
    Last updated:  2014-02-24
    Record count:  4887170
    Field count:   23
    Record length: 235
    --Fields--
      0) respondent I binary
      1) report_yr I binary
      2) report_prd I binary
      3) sys_key I binary
      4) tr_id C(24)
      5) tr_contrac I binary null
      6) tr_begin_d T binary null
      7) tr_end_dat T binary null
      8) tr_timezon C(2) null
      9) tr_delv_ct C(4) null
     10) tr_delv_sp C(48) null
     11) tr_class_n C(4) null
     12) tr_term_na C(4) null
     13) tr_inc_nam C(4) null
     14) tr_inc_pea C(4) null
     15) tr_prod_na C(49) null
     16) tr_quantit B binary null
     17) tr_price B binary
     18) tr_units C(9) null
     19) tr_tot_tra B binary null
     20) tr_tot_tr2 B binary null
     21) tr_other M
     22) tr_revised T binary

     array('c', '\x00\x00')
    16
    (2, 0)
(235, array('c', '      \x8f\x04\x00\x00\xd9\x07\x00\x00\x03\x00\x00\x00\x01\x00\x00\
  x001Q09                    \x04\x00\x00\x001u%\x00\xe5\x03\x00\x00\x8au%\x00\x18
    X&\x05MPPNM PNM Switchyard                                  F   LT  M   FP  CAPA
 CITY                                         \x00\x00\x00\x00\x80+\x18A\xba\xda\
    x8a\xfdew\x0f@$/KW-MO  \x00\x00\x00\x00\x00\x00\x00\x00\xcd\xcc\xcc\xccR\xc47A\x
 00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'))
  ('0', 233, 2, 235, 0, 5, <function none at 0x110DF9B0>, <function none at 0x110D
 F9B0>)
array('c', '\x00\x00')
  Traceback (most recent call last):
  File "dbf_convert_stack.py", line 20, in <module>
DBFtoCSV(path)
  File "dbf_convert_stack.py", line 16, in DBFtoCSV
dbf.export(current_table)
  File "C:\Python27\ArcGIS10.4\lib\site-packages\dbf\ver_2.py", line 7859, in ex
  port
data = record[fieldname]
    File "C:\Python27\ArcGIS10.4\lib\site-packages\dbf\ver_2.py", line 2541, in __
getitem__
return self.__getattr__(item)
  File "C:\Python27\ArcGIS10.4\lib\site-packages\dbf\ver_2.py", line 2508, in __
 getattr__
value = self._retrieve_field_value(index, name)
    File "C:\Python27\ArcGIS10.4\lib\site-packages\dbf\ver_2.py", line 2693, in _r
etrieve_field_value
if ord(null_data[byte]) >> bit & 1:
IndexError: array index out of range

【问题讨论】:

  • 另外,好像不管我选择什么方法,dbf.dbf通常也会给我报错。不知道为什么
  • 那么也许你应该调试你的dbf.Dbf问题并为它写一个minimal reproducible example
  • 我猜你的dbf 文件有点搞砸了。
  • 感谢您的帮助。我将致力于创建该示例并研究 dbf!
  • 我将问题回滚到 dbfpy 错误,因为这就是我的答案所指的。如果您需要添加更多信息,只需将其添加到底部即可。 :)

标签: python csv dbf arcpy


【解决方案1】:

使用 SearchCursor 可以相当简单。您真正需要做的就是获取字段名称,将其传递到光标中,然后使用 Python 的 csv 模块将完整的行写入 csv。

import arcpy
import csv

dbf = table_name # Pass in the table you've identified
outputFile = '{}.csv'.format(dbf.split('.dbf')[0])

# Get the fields in the dbf to use for the cursor and csv header row.
fields = []
for field in arcpy.ListFields(dbf):
    fields.append(field.name)

# Make the csv.
with open(outputFile, 'wb') as output:
    dataWriter = csv.writer(output, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)

    # Write header row.
    dataWriter.writerow(fields)

    # Write each row of data to the csv.
    with arcpy.da.SearchCursor(dbf, fields) as cursor:
        for row in cursor:
            dataWriter.writerow(row)

print('Finished creating {}'.format(outputFile))

【讨论】:

  • 谢谢!我已经复制了您给我的内容并修改了脚本,但出现以下错误:TypeError: Fields must be string or non-empty sequence of strings。很高兴向您展示代码。另一个问题:当您说通过您确定的表时,我应该循环遍历 arcpy.TableList 还是继续使用 dbf.Table
  • 如果可能的话,我会使用 arcpy.TableList。关于您的错误,您的任何字段中是否有异常字符?
  • 是的,一些下划线和数字。除了其他的,还没有全部扫描。我尝试使用 ValidateFieldName,但无法正常工作。有没有使用循环的简单修复方法?
  • 可能有几件事要尝试,但我会首先确保您将字符串推送到字段列表中:fields.append(str(field.name))
  • 我试过了,然后我在循环结束时打印了字段,但由于某种原因,列表空了。试图找出原因。
【解决方案2】:

不要使用dbfpy,而是使用my dbf module

import dbf  # instead of dbfpy

def DBFtoCSV(path):
    '''Convert every DBF table into CSV table. '''
    env.workspace = path
    tablelist = arcpy.ListTables() # list tables in file
    for table in tablelist: # iterate through every table
        #make sure you are just working with .dbf tables 
        if table.endswith('.dbf'):
            with dbf.Table(table) as current_table:
                dbf.export(current_table)
        #keep track of processing
        print "\n Processing ",table[:-4]+".csv table complete."

【讨论】:

  • 谢谢!我在安装您的 dbf 模块时遇到问题。我已经下载了 zip 文件并放入了工作目录,但是当我输入“pip install dbf”时,它找不到该文件。再次感谢您的帮助,我使用 Python 已经 3 年了,那是基本的东西。
  • 查看this answer 寻求帮助(我从未将 pip 与 zip 文件一起使用)。
  • 似乎 IntEnum 不适用于 Python 2.7。当我收到 IntEnum 的导入错误时,是否有任何解决方法。由于我拥有 ARCGIS 的许可软件,出于工作原因,我非常坚持使用 2.7。
  • @jollygood18: IntEnum 应该在那里。你能用你得到的回溯更新你的问题吗?
  • 刚刚做了。用你给我的和我得到的错误更新了代码
猜你喜欢
  • 2011-04-23
  • 2012-02-16
  • 1970-01-01
  • 2020-03-07
  • 1970-01-01
  • 2012-02-09
  • 2011-05-25
  • 2013-08-28
  • 2013-10-13
相关资源
最近更新 更多