Python 列出MXD文件中图层的字段名称
我有一个ArcMap文件(.MXD),我想搜索它的层,然后选择一个层,让Python显示该层属性表的字段名 到目前为止,Python(ArcPy)向我列出了mxd的层名称,但我不知道如何获得字段名称 在ArcMap中,我可以通过以下方式轻松完成:Python 列出MXD文件中图层的字段名称,python,python-2.7,arcpy,Python,Python 2.7,Arcpy,我有一个ArcMap文件(.MXD),我想搜索它的层,然后选择一个层,让Python显示该层属性表的字段名 到目前为止,Python(ArcPy)向我列出了mxd的层名称,但我不知道如何获得字段名称 在ArcMap中,我可以通过以下方式轻松完成: fields = arcpy.ListFields(Layer) for field in fields: print field.name 但是我如何通过MXD文件在ArcMap之外实现这一点呢?我搜索了很多,但什么也没找到,所以我期待着你
fields = arcpy.ListFields(Layer)
for field in fields:
print field.name
但是我如何通过MXD文件在ArcMap之外实现这一点呢?我搜索了很多,但什么也没找到,所以我期待着你的帮助!非常感谢 通过
arcpy.mapping.MapDocument
方法访问mxd。然后获取名称并打开属性表
mxd = arcpy.mapping.MapDocument(r"path/Project.mxd")
for df in arcpy.mapping.ListLayers(mxd):
print df.name
您可以使用arcpy
并通过ListFileds
方法运行python脚本来显示表文件
import arcpy
fieldList = arcpy.ListFields("path/shapefile.shp")
for field in fieldList:
print field.baseName
通过
arcpy.mapping.MapDocument
方法访问mxd。然后获取名称并打开属性表
mxd = arcpy.mapping.MapDocument(r"path/Project.mxd")
for df in arcpy.mapping.ListLayers(mxd):
print df.name
您可以使用arcpy
并通过ListFileds
方法运行python脚本来显示表文件
import arcpy
fieldList = arcpy.ListFields("path/shapefile.shp")
for field in fieldList:
print field.baseName
好的,我找到了一个很好的解决办法。我首先从MXD文件中获取所有层,然后将每个层的名称和源代码保存到字典中。然后,我将从GUI中选择所需的图层,并使用字典中的图层名称进行检查,然后通过字典访问字段名称:
import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\MyMap.mxd") # loads my map
df = arcpy.mapping.ListDataFrames(mxd) # checks out the dataframes
layersources = {} # creates an empty dictionary
for d in df:
layers = arcpy.mapping.ListLayers(mxd, "", d) # lists all available layers
for lyr in layers:
layersources[lyr.name] = lyr.dataSource # fills keys and values of the layers (names and sources) into the dictionary
selecteditem = "the wanted layer" # this I choose from a GUI then, just defined it here as a variable for testing purposes
fields = arcpy.ListFields(layersources[selecteditem]) # creates a list with all the fields from that layer
for field in fields: # iterates through the list of fields
print field.name # and prints them one by one :-)
好的,我找到了一个很好的解决办法。我首先从MXD文件中获取所有层,然后将每个层的名称和源代码保存到字典中。然后,我将从GUI中选择所需的图层,并使用字典中的图层名称进行检查,然后通过字典访问字段名称:
import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\MyMap.mxd") # loads my map
df = arcpy.mapping.ListDataFrames(mxd) # checks out the dataframes
layersources = {} # creates an empty dictionary
for d in df:
layers = arcpy.mapping.ListLayers(mxd, "", d) # lists all available layers
for lyr in layers:
layersources[lyr.name] = lyr.dataSource # fills keys and values of the layers (names and sources) into the dictionary
selecteditem = "the wanted layer" # this I choose from a GUI then, just defined it here as a variable for testing purposes
fields = arcpy.ListFields(layersources[selecteditem]) # creates a list with all the fields from that layer
for field in fields: # iterates through the list of fields
print field.name # and prints them one by one :-)
是的,对于一个形状文件来说,很清楚。关键是用一个MXD来做,然后从那里指向图层。类似于
arcpy.ListFields(mxd,Layer)
,但这不起作用。有什么想法吗?@Khaled ok我添加了迭代MXD中的shapefile的选项,其他解决方案只提供MXD的层。我希望Python打开MXD,给我一个所有层的列表,我从中选择一个(GUI),然后得到该层的字段。但我找到了一个解决办法(见下文)。我不得不欺骗一些人。任何人都有更好的解决方案,欢迎发布!是的,对于一个形状文件来说,很清楚。关键是用一个MXD来做,然后从那里指向图层。类似于arcpy.ListFields(mxd,Layer)
,但这不起作用。有什么想法吗?@Khaled ok我添加了迭代MXD中的shapefile的选项,其他解决方案只提供MXD的层。我希望Python打开MXD,给我一个所有层的列表,我从中选择一个(GUI),然后得到该层的字段。但我找到了一个解决办法(见下文)。我不得不欺骗一些人。任何人都有更好的解决方案,欢迎发布!您是如何在GUI中实现这一点的?您是否使用selecteditem var作为参数或提示,然后作为可供选择的层的下拉列表?您如何在GUI中实现这一点?您是否使用selecteditem var作为参数或提示,然后作为要选择的图层的下拉列表?