1.背景
今天接了个任务,需要检查数据库中要素类属性字段的规范性,由于数据库众多,格式相对规范,现编写代码实现批量检查。
2. 搭建环境
- PyScripter Version 3.6.1.0 X86 © Kiriakos Viahos 2005-2019
- Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32.
- Excel 2013 (以上版本)
- 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中
程序不是很规范,需进一步优化

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

1794

被折叠的 条评论
为什么被折叠?



