【问题标题】:Importing data from dbf to excel spreadsheet using Wing IDE使用 Wing IDE 将数据从 dbf 导入到 excel 电子表格
【发布时间】:2012-08-16 16:29:28
【问题描述】:

我正在尝试将一列数据(平均值)从 dbf 文件移动到 Excel 电子表格。到目前为止,我一直在使用 Wing IDE 进行尝试,但没有成功。我不是编程学生,这是一项短期作业。我被困在必须从特定网络驱动器检索文件并将数据复制到本地 Excel 工作表的部分。帮助会很棒。谢谢

【问题讨论】:

  • 是的。 dBase。文件的结尾在 dbf 中。我到目前为止的代码是:

    import os, os.path, arcgisscripting
    from dbfpy import dbf
    filepath = os.path.join(os.path.dirname( "E:/Yinsuo/MODIS/EVI.NDVI.STATS/"))
    outfile = os.path.join(os.path.dirname("E:/Syed Workspace/Yinsuo.xlsx")) 用于 IntFiles在 os.listdir(filepath):
    if IntFiles.find("EVI.dbf") != -1:
    db = dbf.Dbf(filepath + "/" + IntFiles)
    打印数据库
  • 您知道您只需选择文件->打开并选择 *.dbf 作为文件类型即可在 Excel 中打开 dBase 文件?无需导入或任何要求。
  • 我需要从数百个不同的 dbf 文件中获取数据并将其编译到一个 Excel 工作表中。一次做这件事会花很长时间。

标签: python excel dbf wing-ide


【解决方案1】:

您需要Python Excel 工具,我也会推荐我自己的dbf package

import dbf
import xlwt

dbf_files = ('file1.dbf','file2.dbf','file3.dbf')

output_xls = xlwt.Workbook()
sheet = output_xls.add_sheet('sheet_name')

for i, filename in enumerate(dbf_files):
    total = 0
    with dbf.Table(filename) as table:
        for record in table:
            total += record.some_count   # some_count being a field name in the dbf files
    sheet.write(i, 0, filename)
    sheet.write(i, 1, total)

output_xls.save('final.xls')

希望这能让您了解如何处理您的用例。如果您有任何问题,请告诉我。

【讨论】:

  • 我在使用 dbf 文件中的字段名称代替 some_count 时遇到问题。 dbf 文件来自 ArcGIS,并且 ArcGIS 中显示的每个字段名称都被拒绝为 FieldMissingError: 'FID: no such field in table'。在这种情况下,FID 是我文件中每一行的 ID。
  • @CF84:字段名称只能以小写形式访问(所以fid 而不是FID
  • 与上述相同的错误。读取 dbf 文件后如何查看字段是什么?
  • @CF84: print 表,或dbf.field_names(thing) 其中thing 是表或记录。
  • @CF84:没那么容易。如果您提出新问题,我很乐意回答。在 cmets 中很难放代码。
【解决方案2】:

据我了解,您可以将 ADODB 与 Python 一起使用。您可以针对连接运行查询以从 DBF 插入 Excel 文件。

这适用于VBA,希望你能翻译。

strCon = "Provider=Microsoft.ACE.OLEDB.12.0;" _
  & "Data Source=z:\docs\myexcel.xlsm;Extended Properties=""Excel 8.0;HDR=No"";"

Set cn = CreateObject("ADODB.Connection")
cn.Open strCon

strsql = "SELECT * INTO [mynewsheet] " _
  & "FROM [dBASE III;DATABASE=z:\docs\].[mydbf.dbf] "
  cn.Execute strsql

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-27
    • 1970-01-01
    • 1970-01-01
    • 2014-07-22
    • 1970-01-01
    • 1970-01-01
    • 2011-12-14
    相关资源
    最近更新 更多