ArcGIS清除shp 字段值得前后空格

使用函数

strip()

工具详细代码

 

import sys 
reload(sys) 
sys.setdefaultencoding("utf-8")
import arcpy,os
arcpy.env.workspace = arcpy.GetParameterAsText(0)
ResultData =arcpy.GetParameterAsText(1)
Features = arcpy.ListFeatureClasses()
arcpy.AddMessage("{0}".format("Start clearing"))
for Feature in Features:
    OutCopyFeature = os.path.join(ResultData, os.path.basename(Feature))    
    outID = OutCopyFeature
    outNCHN = outID
    outNENG = outNCHN
    outYEAR = outNENG
    outFLAGP = outYEAR
    outFLAGN = outFLAGP
    outPERIOD = outFLAGN
    arcpy.CopyFeatures_management(Feature, OutCopyFeature, "", "0", "0", "0")
    arcpy.CalculateField_management(OutCopyFeature, "I", "!I!.strip()", "PYTHON_9.3", "")
    arcpy.CalculateField_management(outID, "NA", "!NA!.strip()", "PYTHON_9.3", "")
    arcpy.CalculateField_management(outNCHN, "NE", "!NE!.strip()", "PYTHON_
ArcGIS中编写一个shp转txt的脚本可以使用Python语言来实现,具体步骤如下: 1. 打开ArcGIS,点击“工具”菜单,选择“Python”窗口; 2. 在Python窗口中输入以下代码: ```python import arcpy # 输入shp文件路径和txt文件路径 in_shp = r"C:\path\to\input.shp" out_txt = r"C:\path\to\output.txt" # 自定义输出坐标系 out_coor_system = arcpy.SpatialReference("WGS 1984") # 自定义生产单位、时间和shp字段信息 production_unit = "xxx" time = "2021-01-01" field_info = "field1,field2,field3" # 创建游标,读取shp中的要素 cursor = arcpy.da.SearchCursor(in_shp, ["SHAPE@XY"] + field_info.split(",")) # 打开输出文件,写入标题行 with open(out_txt, "w") as f: f.write("X,Y,%s,ProductionUnit,Time\n" % field_info) # 遍历每个要素,写入坐标和属性信息 for row in cursor: x, y = row[0] attrs = ",".join(str(a) for a in row[1:]) f.write("%.6f,%.6f,%s,%s,%s\n" % (x, y, attrs, production_unit, time)) # 完成后打印提示信息 print("转换完成!") ``` 3. 将代码中的`in_shp`和`out_txt`分别替换为要转换的shp文件路径和输出的txt文件路径; 4. 根据需要自定义输出坐标系、生产单位、时间和shp字段信息,分别替换代码中对应的变量; 5. 运行代码,在Python窗口中或者调用脚本文件运行即可。 注意事项: - 如果要输出其他坐标系的文件,需要在`arcpy.SpatialReference()`中输入对应的坐标系信息; - 如果要输出其他类型的文件,比如csv或者xlsx,需要修改代码中的文件打开方式和写入方式; - 如果shp文件中的字段名中有空格或者特殊字符,需要在代码中将其用双引号括起来,比如`"field name"`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值