python存储CSV二维数据——利用arcpy进行要素差分量统计
# coding=utf-8
#要素差分量统计
import arcpy
#from arcpy import env
f = open('count.csv','w')
title = ['CITY','GREENURBAN_SymDiff','WATER_SymDiff']
f.write(','.join(title)+'\n')
#全部城市60个,不包括香港、澳门 City_Dictionary
city_names={#配合arcpy包使用python2.7,注意语法
u'三亚':'SANYA',
u'上海':'SHANGHAI',
u'东莞':'DONGGUAN',
u'中山':'ZHONGSHAN',
u'乌鲁木齐':'WULUMUQI',
u'佛山':'FOSHAN',
u'保定':'BAODING',
u'兰州':'LANZHOU',
u'北京':'BEIJING',
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'广州':'GUANGZHOU',
u'徐州':'XUZHOU',
u'惠州':'HUIZHOU',
u'成都':'CHENGDU',
u'扬州':'YANGZHOU',
u'无锡':'WUXI',
u'昆明':'KUNMING',
u'杭州':'HANGZHOU',
u'柳州':'LIUZHOU',
u'武汉':'WUHAN',
u'沈阳':'SHENYANG',
u'泉州':'QUANZHOU',
u'济南':'JINAN',
u'海口':'HAIKOU',
u'深圳':'SHENZHEN',
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'
}
#统计差分量——由于字典不是序列类型,统计顺序随机
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)
转载自:https://blog.csdn.net/lzqg1990/article/details/88765175