具体代码为:stringold.replace(strfrom,strto),其中stringold就是需要更改的字符串,strfrom是需要替换的子字符串,strto是需要转换成的子字符串。Python是一种跨平台的计算机程序设计语言,也是一种面向对象的动态类型语言,最初被设计用于编写自动化脚本。随着版本的不断更新和语言新功能的添加,越来越多被用于独立的.大型项目的开发。Python语言具有简洁性.易读性以及可扩展性,在国外用Python做科学计算的研究机构日益增多,一些知名大学已经采用Python来教授程序设计课程。
取一个list的部分元素是非常常见的操作。比如,一个list如下:>>>L = ['Adam', 'Lisa', 'Bart', 'Paul']
取前3个元素,应该怎么做?
对这种经常取指定索引范围的操作,用循环十分繁琐,因此,Python提供了切片(Slice)操作符,能大大简化这种操作。
对应上面的问题,取前3个元素,用一行代码就可以完成切片:
>>>L[0:3]
['Adam', 'Lisa', 'Bart']
L[0:3]表示,从索引0开始取,直到索引3为止,但不包括索引3。即索引0,1,2,正好是3个元素。
只用一个 : ,表示从头到尾:
>>>L[:]
['Adam', 'Lisa', 'Bart', 'Paul']
因此,L[:]实际上复制出了一个新list。
切片操作还可以指定第三个参数:
>>>L[::2]
['Adam', 'Bart']
第三个参数表示每N个取一个,上面的 L[::2] 会每两个元素取出一个来,也就是隔一个取一个。
把list换成tuple,切片操作完全相同,只是切片的结果也变成了tuple。
# -*- coding: UTF-8 -*-
#scrip
print"使用shp数据批量裁剪栅格数据并统计结果均值"
###########修改部分###########
ras_file= r"D:\python\nc\GLEAM\Transpiration_TIF_OUT" #待裁剪的原始栅格数据存储文件夹路径
suffix= 'tif' #待裁剪的原始栅格数据后缀
bvalue=-999.0 #待裁剪的原始栅格数据背景值
shp_file= r"D:\python\shp" #裁剪所需shp模板数据存储文件夹路径
ras_file_cut= r"D:\python\nc\GLEAM\Transpiration_YR_SWAT_mask" #裁剪后栅格数据结果存储文件夹路径
txtname=r"D:\python\nc\GLEAM\Transpiration_YR_SWAT_mask" #输出统计文本路径
#############################
#计算部分
import fenqutongji_arcpy
fenqutongji_arcpy.env.workspace=shp_file
shps=fenqutongji_arcpy.ListFeatureClasses()
fenqutongji_arcpy.env.workspace=ras_file
ras=fenqutongji_arcpy.ListRasters('*', suffix)
print "共有"+str(len(ras))+"个栅格数据"
#
print "Processing......"
for shin shps:
shtmp=sh.encode('cp936')
shpfile=shp_file+"\\"+shtmp
print "共有"+str(len(shps))+"个shp数据,正在处理第"+str(shps.index(sh)+1)+"个:"+shtmp
result=[]
for rsin ras:
rstmp=rs.encode('cp936')
outname=ras_file_cut+"\\"+rstmp[0:len(rstmp)-4]+shtmp[0:len(shtmp)-4]+".tif"
#arcpy.Clip_management(rstmp,"#",outname,shpfile,"#","ClippingGeometry")
fenqutongji_arcpy.Clip_management(rstmp, "#", outname, shpfile, str(bvalue), "ClippingGeometry")#忽略无效值
stats=fenqutongji_arcpy.GetRasterProperties_management(outname, "MEAN")
result.append(rstmp+' '+str(stats)+"\n")
# try:
# arcpy.Delete_management(outname,"")#注释则裁剪的栅格不删除,不注释删掉。
# except:
print(outname+'cannot delete')
file(txtname+"\\"+shtmp[0:len(shtmp)-4]+".txt",'w').writelines(result)
print "Finish!"