爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 5290|回复: 1

dbf convert to csv using AcrGIS_Python

[复制链接]
发表于 2015-9-25 23:35:12 | 显示全部楼层 |阅读模式

登录后查看更多精彩内容~

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
import os
import arcpy
import csv
def dbf2csv(dbfpath, csvpath):
    ''' To convert .dbf file or any shapefile/featureclass to csv file
    Inputs:
        dbfpath: full path to .dbf file [input] or featureclass
        csvpath: full path to .csv file [output]

    '''
    #import csv
    rows = arcpy.SearchCursor(dbfpath)
    csvFile = csv.writer(open(csvpath, 'wb')) #output csv
    fieldnames = [f.name for f in arcpy.ListFields(dbfpath)]

    allRows = []
    for row in rows:
        rowlist = []
        for field in fieldnames:
            rowlist.append(row.getValue(field))
        allRows.append(rowlist)

    csvFile.writerow(fieldnames)
    for row in allRows:
        csvFile.writerow(row)
    row = None
    rows = None
dbf_dir = 'E:/model_data/LAI'
csv_dir = 'E:/model_data/LAI/result'
for dbf_file in os.listdir(dbf_dir):
    # Loop through all dbf files
    # and export to dbf
    fileName, fileExt = os.path.splitext(dbf_file)  #[0] or [1] for file
    if '.dbf' in fileExt:
        # construct full path to dbf file and csv file
        dbfpath = os.path.join(dbf_dir, fileName+fileExt)
        csvpath = os.path.join(csv_dir, fileName+'.csv')
        if os.path.exists(dbfpath):
            # this may not be necessary
            #    print 'processing: ', dbfpath, csvpath                    
            if not os.path.exists(csvpath):
                ## to prevent overwrite of existing csv file
                ## call the function to convert .dbf file to csv file
                print 'Export nexrad {0} to {1}'.format(dbfpath, csvpath)
                dbf2csv(dbfpath, csvpath)   
密码修改失败请联系微信:mofangbao
发表于 2017-6-23 11:18:11 | 显示全部楼层
谢谢分享,学习学习!!
密码修改失败请联系微信:mofangbao
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Copyright ©2011-2014 bbs.06climate.com All Rights Reserved.  Powered by Discuz! (京ICP-10201084)

本站信息均由会员发表,不代表气象家园立场,禁止在本站发表与国家法律相抵触言论

快速回复 返回顶部 返回列表