输入文件名中英文不一致——字典
#coding = UTF-8
city_names={#配合arcpy包使用python2.7,注意语法
u'三亚':'SANYA',
u'东莞':'DONGGUAN',
u'中山':'ZHONGSHAN',
u'乌鲁木齐':'WULUMUQI',
u'佛山':'FOSHAN',
u'保定':'BAODING',
u'兰州':'LANZHOU',
u'南京':'NANJING',
u'南宁':'NANNING',
u'南昌':'NANCHANG',
u'南通':'NANTONG',
u'厦门':'XIAMEN',
u'台州':'TAIZHOU',
u'合肥':'HEFEI',
u'呼和浩特':'HUHEHAOTE',
u'哈尔滨':'HAERBIN',
u'唐山':'TANGSHAN',
u'大连':'DALIAN',
u'天津':'TIANJIN',
u'太原':'TAIYUAN',
u'宁波':'NINGBO',
u'常州':'CHANGZHOU',
u'徐州':'XUZHOU',
u'惠州':'HUIZHOU',
u'成都':'CHENGDU',
u'扬州':'YANGZHOU',
u'无锡':'WUXI',
u'昆明':'KUNMING',
u'杭州':'HANGZHOU',
u'柳州':'LIUZHOU',
u'武汉':'WUHAN',
u'泉州':'QUANZHOU',
u'济南':'JINAN',
u'海口':'HAIKOU',
u'温州':'WENZHOU',
u'潍坊':'WEIFANG',
u'烟台':'YANTAI',
u'珠海':'ZHUHAI',
u'盐城':'YANCHENG',
u'石家庄':'SHIJIAZHUANG',
u'福州':'FUZHOU',
u'秦皇岛':'QINHUANGDAO',
u'绍兴':'SHAOXING',
u'芜湖':'WUHU',
u'苏州':'SUZHOU',
u'西宁':'XINING',
u'西安':'XIAN',
u'贵阳':'GUIYANG',
u'郑州':'ZHENGZHOU',
u'重庆':'CHONGQING',
u'金华':'JINHUA',
u'银川':'YINCHUAN',
u'长春':'CHANGCHUN',
u'长沙':'CHANGSHA',
u'青岛':'QINGDAO',
u'沈阳':'SHENYANG',
u'广州':'GUANGZHOU',
u'上海':'SHANGHAI',
u'深圳':'SHENZHEN',
u'北京':'BEIJING',
u'香港':'XIANGGANG',
u'澳门':'AOMEN'
}
city_dict2 = dict(zip(city_names.values(), city_names.keys()))
##print(city_names.keys())
##print(list(city_names.keys()))
# coding=utf-8
#要素差分量统计
import arcpy
from City_Dictionary_u import city_names,city_dict2#中英,英中
f = open('count.csv','w')
title = ['CITY','GREENURBAN_SymDiff','WATER_SymDiff']
f.write(','.join(title)+'\n')
#统计差分量——由于字典不是序列类型,统计顺序随机
num = 0
ls = []
for city_k,city_v in city_names.items(): #key:city
#arcpy.GetCount_management()计算差分量
print city_v+u' is counting...'
shpfile_GREENURBAN = 'D:/19Q2G2_DATA/BACK/19Q2_RESULT/19Q2'+city_k+u'绿地差分量.shp'
shpfile_WATER = 'D:/19Q2G2_DATA/BACK/19Q2_RESULT/19Q2'+city_k+u'水系差分量.shp'
result_GREENURBAN = arcpy.GetCount_management(shpfile_GREENURBAN)
result_WATER = arcpy.GetCount_management(shpfile_WATER)
count_GREENURBAN = int(result_GREENURBAN.getOutput(0))
count_WATER = int(result_WATER.getOutput(0))
#差分量计入ls,ls的60个元素是:[城市名,绿地差分量,水系差分量]——3元素列表
ls.append([city_v,str(count_GREENURBAN),str(count_WATER)])
##ls = ls.append()错误:
##append会修改ls本身,并且返回None。不能把返回值再赋值给ls。
##count_GREENURBAN需要转成字符串——str(),否则会报错:
##f.write(','.join(row)+'\n') #TypeError: sequence item 0: expected string, list found
##TypeError: sequence item 1: expected string or Unicode, int found
#写入csv
for row in ls:
#print(row) row=[city_v,city_v,city_v]
#str.join(seq),seq期望是序列类型数据:字符串或列表
f.write(','.join(row)+'\n') #TypeError: sequence item 0: expected string, list found
print '{} recorded!'.format(row[0])
num += 1
print '{} cities successed!'.format(num)
输入文件名统一英文后:
# coding=utf-8
import arcpy
from City_Dictionary_u import city_dict2#全部城市60个,不包括香港、澳门 City_Dictionary
f = open('count.csv','w')
title = ['CITY','GREENURBAN_SymDiff','WATER_SymDiff']#统计结果cxv文件表头
f.write(','.join(title)+'\n')
city_e_seq_ls =['BEIJING', 'SHANGHAI', 'GUANGZHOU', 'SHENZHEN', 'SHENYANG', 'NANJING',\
'HANGZHOU', 'TIANJIN', 'CHENGDU', 'XIAN', 'SUZHOU', 'ZHENGZHOU', 'WUHAN',\
'DALIAN', 'WUXI', 'NINGBO', 'HK', 'CHANGCHUN', 'CHANGSHA', 'WENZHOU',\
'JINAN', 'TAIYUAN', 'HEFEI', 'DONGGUAN', 'XIAMEN', 'HAERBIN', 'KUNMING',\
'CHONGQING', 'QINGDAO', 'TANGSHAN', 'FOSHAN', 'FUZHOU', 'MC', 'MCZH', 'NANNING',\
'HUHEHAOTE', 'LANZHOU', 'YANGZHOU', 'SHIJIAZHUANG', 'QUANZHOU', 'YANTAI',\
'NANCHANG', 'WULUMUQI', 'GUIYANG', 'BAODING', 'HAIKOU', 'SHAOXING', 'TAIZHOU',\
'SANYA', 'LIUZHOU', 'QINHUANGDAO', 'XINING', 'ZHUHAI', 'WEIFANG', 'XUZHOU',\
'YANCHENG', 'NANTONG', 'JINHUA','CHANGZHOU', 'HUIZHOU', 'ZHONGSHAN', 'YINCHUAN', 'WUHU']
#统计差分量——由于字典不是序列类型,统计顺序随机
num = 0
ls = []
#for city_k,city_v in city_names.items(): #key:city
for city in city_e_seq_ls:
#arcpy.GetCount_management()计算差分量
print city+u' is counting...'
shpfile_GREENURBAN = r'D:\20Q2M2\DATA\20Q2M2G2\20Q2M2_BACK_SYMDIFF\\20Q2M2'+city_dict2.get(city,"null")+u'绿地差分量.shp'
try:
result_GREENURBAN = arcpy.GetCount_management(shpfile_GREENURBAN)
count_GREENURBAN = str(int(result_GREENURBAN.getOutput(0)))
except:
count_GREENURBAN = "null"
shpfile_WATER = r'D:\20Q2M2\DATA\20Q2M2G2\20Q2M2_BACK_SYMDIFF\\20Q2M2'+city_dict2.get(city,"null")+u'水系差分量.shp'
try:
result_WATER = arcpy.GetCount_management(shpfile_WATER)
count_WATER = str(int(result_WATER.getOutput(0)))
except:
count_WATER = "null"
#差分量计入ls,ls的60个元素是:[城市名,绿地差分量,水系差分量]——3元素列表
ls.append([city,count_GREENURBAN,count_WATER])
#写入csv
for row in ls:
f.write(','.join(row)+'\n') #2
print '{} recorded!'.format(row[0])
num += 1
f.close()
print '{} cities successed!'.format(num)
#coding=utf-8
import arcpy
import os
#city_e_seq_ls:与《预处理变化量统计表》顺序匹配的英文城市名列表 63-香港-澳门-珠澳/珠海
#统计差分量——由于字典不是序列类型,统计顺序随机,这里使用列表
city_e_seq_ls =['BEIJING', 'SHANGHAI', 'GUANGZHOU', 'SHENZHEN', 'SHENYANG', 'NANJING',\
'HANGZHOU', 'TIANJIN', 'CHENGDU', 'XIAN', 'SUZHOU', 'ZHENGZHOU', 'WUHAN',\
'DALIAN', 'WUXI', 'NINGBO', 'HK', 'CHANGCHUN', 'CHANGSHA', 'WENZHOU',\
'JINAN', 'TAIYUAN', 'HEFEI', 'DONGGUAN', 'XIAMEN', 'HAERBIN', 'KUNMING',\
'CHONGQING', 'QINGDAO', 'TANGSHAN', 'FOSHAN', 'FUZHOU', 'MC', 'MCZH', 'NANNING',\
'HUHEHAOTE', 'LANZHOU', 'YANGZHOU', 'SHIJIAZHUANG', 'QUANZHOU', 'YANTAI',\
'NANCHANG', 'WULUMUQI', 'GUIYANG', 'BAODING', 'HAIKOU', 'SHAOXING', 'TAIZHOU',\
'SANYA', 'LIUZHOU', 'QINHUANGDAO', 'XINING', 'ZHUHAI', 'WEIFANG', 'XUZHOU',\
'YANCHENG', 'NANTONG', 'JINHUA','CHANGZHOU', 'HUIZHOU', 'ZHONGSHAN', 'YINCHUAN', 'WUHU']
f = open('count_POI.csv','w')
title = ['CITY','POI','POI_ADD','POI_DEL','POI_INVARI']#需与dirs列表元素顺序一致
f.write(','.join(title)+'\n')
ls = []#存放63个元素(城市名及对应的4种统计值的列表)的列表
#拼出POI——4种需要统计的数据路径
root = r'D:\20Q2\00DATA\BACK\POI\POI_4'#根目录,含中文加u
paths = os.listdir(root)
print paths
dirs = []#POI4种路径的列表
for path in paths:
dirs.append(os.path.join(root,path))
print dirs #打印出统计顺序并检查与title是否一致
for city_e in city_e_seq_ls:
print city_e+' is counting...'
ls_4 = []
for i in dirs:
#arcpy.GetCount_management()获取shp属性表行数
shpfile_POI =i+ '/' + city_e + '_OUTLINE_IX_POI.shp'
try:
result_POI = arcpy.GetCount_management(shpfile_POI)
count_POI = str(int(result_POI.getOutput(0)))
except:
count_POI ='null'
#把每个城市的4种统计数据存入列表ls_4
ls_4.append(count_POI)
#每个城市的【英文名与4种统计数据ls_4】作为元素,存入ls
ls.append([city_e]+ls_4)
#ls_4.clear()
#写入csv
num = 0
for row in ls:
#print(row) row=['CITY','POI','POI_ADD','POI_DEL','POI_INVARI']
#str.join(seq),seq期望是序列类型数据:字符串或列表
f.write(','.join(row)+'\n') #TypeError: sequence item 0: expected string, list found
print '{} recorded!'.format(row[0])
num += 1
f.close()
print '{} cities successed!'.format(num)