首先声明,这篇代码是改自他处,链接是:
https://blog.csdn.net/liyanzhong/article/details/44935885 《python获取栅格点和面值》
直接贴修改后的代码:
import arcpy
OutputFile = open('statisticResult.txt', 'w')
arcpy.env.workspace = ('G:\Global_P_ET\P_extraction')
rlist = arcpy.ListRasters('*','tif')
for file in rlist:
meanValueInfo = arcpy.GetRasterProperties_management(file,'MEAN')
meanValue = meanValueInfo.getOutput(0)
print file + ',' + str(meanValue) +'\n'
OutputFile.write(file + ',' + str(meanValue) +'\n')
OutputFile.close()
print("All done")
与源代码相比,增加了表头,修改了部分拼写错误及格式错误(Python中没有分号,用严格的缩进来表示上下级从属关系),输出文件设置为了txt格式,以便于传到EXEL中去
关于open函数,另附一篇文章,其中有详细讲解,希望对你有帮助!
https://www.cnblogs.com/zacharyVic/p/8940143.html 《python open 关于读、写、追加的总结》