Arcpy之要素字段属性获取并写入xls

本文介绍了一个用于检查数据库中要素类属性字段规范性的Python脚本工具。该工具通过遍历指定数据库中的要素,并将检查结果输出到Excel文件中,适用于批量处理多个数据库。


1.背景

今天接了个任务,需要检查数据库中要素类属性字段的规范性,由于数据库众多,格式相对规范,现编写代码实现批量检查。


2. 搭建环境

  1. PyScripter Version 3.6.1.0 X86 © Kiriakos Viahos 2005-2019
  2. Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32.
  3. Excel 2013 (以上版本)
  4. Arcgis10.22

3.思路

1.遍历要素路径并写入txt(可以手动创建,也可用程序遍历创建)
2.循环读取txt内的要素路径
3.检查要素的字段属性信息
4.将检查结果输出到xls文件

4.功能介绍

1.检查的字段的内容包括:要素类名称、字段名称、别称、字段类型、字段长度、要素类型、要素路径、坐标系
2.将检查的出的数据结果写入到xls中

5.使用步骤

5.1 导入库

#coding:utf8
import os
import arcpy.da
import xlwt              #pip install xlwt
import logging           

5.2 功能代码


#日志文件格式--打印处理信息格式
logging.basicConfig(level=logging.DEBUG,format='%(message)s')
class Err(Exception):
    pass

##遍历要素并写入到txt
def traverseFeatureClass(path,txtpath):
    arcpy.env.workspace =path
    featureclasses = arcpy.ListFeatureClasses()#feature_type='Polygon'
    with open (txtpath,'w') as f:
        for fc in featureclasses:
            joinFeaturePath=os.path.join(path,fc)
            f.write(joinFeaturePath.encode('utf-8')+'\n')       #-----解决如何将中文存储在txt中
        f.close()

#读取txt文件内容
def Readtext(path):
    FList=[]
    with open (path,'r') as f :# 源代码
##        for line in f.readlines():
        for line in f:#文件本身就是迭代器,优化代码,提高速度
            #l=line.strip('\n')
            l=line.replace("\xef\xbb\xbf","").strip('\n')
            FList.append(l)
    return FList


workFilePath=Readtext(u"D:\Desktop\数据检查结果\数据库工作路径_格式文本.txt")


count=0
#输入变量
for path in workFilePath:
    txtpath=unicode(r"D:\Desktop\数据检查结果\数据库中的所有要素路径%d.txt"%count,'utf-8')
    excelpath=unicode(r'D:\Desktop\数据检查结果\字段信息%d.xls'%count,'utf-8')    ##'ascii' codec can't decode byte 0xe9 in position 8: ordinal not in range(128)    excel的后缀使用*.xlsx
    count+=1
    featurepath=txtpath
    traverseFeatureClass(path,txtpath)

    ##创建Excel
    xls = xlwt.Workbook(encoding='utf-8')
    ##设置工作表的名称
    sht1 = xls.add_sheet(u'字段信息表')
    b=Readtext(featurepath)
    countFeature=len(b)
    ##数据标题写入
    #Excel表格中:
    # 列明名称     A            B        C       D           E          F          G           H
    FieldList=[u'要素类名称',u'字段名称',u'别称',u'字段类型',u'字段长度',u'要素类型',u'要素路径',u'坐标系']
    for column in range(len(FieldList)):
        sht1.write(0,column,FieldList[column])
    row = 0
    for i in range(countFeature):
        ##要素路径
        FeatureClass=b[i]
        try:
            FieldList=arcpy.ListFields(FeatureClass)
            FieldLen=len(FieldList)
            for index,field in enumerate(FieldList):
                featureclasspath=os.path.join(path,FeatureClass)
                row += 1
                #要素名称
                sht1.write(row,0,str(FeatureClass.split('\\')[-1]))
                #字段名称
                sht1.write(row,1,field.name)
                #字段别称
                sht1.write(row,2,field.aliasName)
                #字段类型
                sht1.write(row,3,field.type)
                #字段长度
                sht1.write(row,4,field.length)
                #要素类型
                sht1.write(row,5,arcpy.Describe(featureclasspath).shapetype)
                #要素路径
                sht1.write(row,6,str(featureclasspath.encode('utf-8')))
                #坐标系
                sht1.write(row,7,arcpy.Describe(featureclasspath).spatialReference.name)

                logging.debug(u"INFO# 共计 %s个要素类,检索到第%s个要素类(已经记录至%s/共%s个字段)" %(countFeature,i+1,index+1,FieldLen))
        except Exception as e:
            print(e.message)

    ##    保存在当前文件夹下的Excel表格
    try:
        logging.info(u'开始保存到Excel中...')
        xls.save(excelpath)
        logging.info(u'程序运行结束,字段信息已经保存Excel中:'+excelpath)
    except Exception,arg:
        print 'Msg:'+str(arg)
        raise Err()

6.总结

实现对数据库内多个要素规范性检查
根据此代码还可以修改为对数据的其他属性进行检查并保存到xls中
程序不是很规范,需进一步优化

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

激动的兔子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值