Python - 将 CSV 转换为 DBF

发布于 2024-09-26 11:12:23 字数 933 浏览 8 评论 0原文

我想使用 python 将 csv 文件转换为 dbf (用于地理编码,这就是我需要 dbf 文件的原因)-我可以在 stat/transfer 或其他类似程序中轻松执行此操作,但我想作为我的一部分脚本,而不必转到外部程序。似乎有很多关于将 DBF 转换为 CSV 的帮助问题/答案,但我没有任何运气。

使用 dbfpy 的答案很好,我只是没有运气确切地弄清楚如何做到这一点。

作为我正在寻找的示例,这里是我在网上找到的一些用于将 dbf 转换为 csv 的代码:

import csv,arcgisscripting
from dbfpy import dbf

gp = arcgisscripting.create()

try:
    inFile = gp.GetParameterAsText(0) #Input
    outFile = gp.GetParameterAsText(1)#Output
    dbfFile = dbf.Dbf(open(inFile,'r'))
    csvFile = csv.writer(open(outFile, 'wb'))
    headers = range(len(dbfFile.fieldNames))
    allRows = []
    for row in dbfFile:
        rows = []
        for num in headers:
            rows.append(row[num])
        allRows.append(rows)
    csvFile.writerow(dbfFile.fieldNames)
    for row in allRows:
        print row
        csvFile.writerow(row)
except:
    print gp.getmessage()

如果能得到类似的东西来实现相反的效果,那就太好了。

谢谢你!

I would like to convert a csv file to dbf using python (for use in geocoding which is why I need the dbf file) - I can easily do this in stat/transfer or other similar programs but I would like to do as part of my script rather than having to go to an outside program. There appears to be a lot of help questions/answers for converting DBF to CSV but I am not having any luck the other way around.

An answer using dbfpy is fine, I just haven't had luck figuring out exactly how to do it.

As an example of what I am looking for, here is some code I found online for converting dbf to csv:

import csv,arcgisscripting
from dbfpy import dbf

gp = arcgisscripting.create()

try:
    inFile = gp.GetParameterAsText(0) #Input
    outFile = gp.GetParameterAsText(1)#Output
    dbfFile = dbf.Dbf(open(inFile,'r'))
    csvFile = csv.writer(open(outFile, 'wb'))
    headers = range(len(dbfFile.fieldNames))
    allRows = []
    for row in dbfFile:
        rows = []
        for num in headers:
            rows.append(row[num])
        allRows.append(rows)
    csvFile.writerow(dbfFile.fieldNames)
    for row in allRows:
        print row
        csvFile.writerow(row)
except:
    print gp.getmessage()

It would be great to get something similar for going the other way around.

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月亮坠入山谷 2024-10-03 11:12:23

重复问题位于:使用 Python 将 .csv 文件转换为 .dbf?

有希望的答案(除其他外)是
使用 csv 库 从 csv 文件读取数据。第三方dbf库可以为您编写dbf文件。

Duplicate question at: Convert .csv file into .dbf using Python?

Promising answer there (among others) is
Use the csv library to read your data from the csv file. The third-party dbf library can write a dbf file for you.

丘比特射中我 2024-10-03 11:12:23

例如,您可以尝试:

您也可以在 OpenOffice 或 Excel 中打开 CSV 文件并将其保存为 dBase 格式。

我假设您想为 Esri Shapefile 格式或类似格式创建属性文件。请记住,DBF 文件通常使用古老的字符编码,例如 CP 850。如果您的地理数据包含外语名称,这可能会出现问题。但是,Esri 可能指定了不同的编码。

编辑:刚刚注意到您不想使用外部工具。

For example, you could try:

You could also just open the CSV file in OpenOffice or Excel and save it in dBase format.

I assume you want to create attribute files for the Esri Shapefile format or something like that. Keep in mind that DBF files usually use ancient character encodings like CP 850. This may be a problem if your geo data contains names in foreign languages. However, Esri may have specified a different encoding.

EDIT: just noted that you do not want to use external tools.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文