Python - 将 CSV 转换为 DBF
我想使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
重复问题位于:使用 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.
例如,您可以尝试:
您也可以在 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.