import sys
reload(sys)
sys.setdefaultencoding(""utf-8"") #应用utf码,默认是ascii码
import arcpy
path="D:/GISData"
arcpy.env.workspace=path
arcpy.MakeFeatureLayer_management(path+"/YF/样方分布图.shp",""YF"")#将样方导入图层,定名YF
arcpy.MakeFeatureLayer_management(path+"/QJ/区界.shp",""QJ"")#将区界导入图层,定名QJ
qjrws=arcpy.SearchCursor("QJ") #遍历**市各个区县
fds=[""YFBH"",""YFXS"",""Area"",""GDLX"",""YFDZ""] #须要记录样方的属性名称
df=open(path+"/data.txt",""a"") #筹办将数据写入data文件
for fd in fds:
df.write(fd.ljust(9))
df.write(""\t"")
df.write(""\n"")
for qjrw in qjrws:
arcpy.SelectLayerByAttribute_management("QJ","NEW_SELECTION","XZQHMC ="""+qjrw.getValue(""XZQHMC"")+"""")#依次选择每个区县
arcpy.SelectLayerByLocation_management ("YF","COMPLETELY_WITHIN","QJ")#选择每个区县里面的样方
df.write(""\n"")
rows=arcpy.SearchCursor("YF") #遍历选中的每个样方
df.write(qjrw.getValue(""XZQHMC"")) #写入区县名称
df.write(""\n"")
df.write(""\n"")
for r in rows: #依次写入每个样方的数据
for fd in fds:
df.write(str(r.getValue(fd)).ljust(9))
df.write(""\t"") #制表排版
df.write(""\n"")#换行
df.close()#封闭文件
文件结果 YFBH YFXS Area GDLX YFDZ
**区(县)
9020116 12.19 19404.02 梯田(T) **区(县)**镇(乡)**村*组 9020202 14.41 22293.12 坡地(P) **区(县)**镇(乡)**村*组 9020210 10.42 15362.01 坡地(P) **区(县)**镇(乡)**村*组 9020214 11.09 24548.32 坡地(P) **区(县)**镇(乡)**村*组 9020232 15.28 20627.35 坡地(P) **区(县)**镇(乡)**村*组 9020430 17.06 11893.23 坡地(P) **区(县)**镇(乡)**村*组