Usually one can get the list of field/column names using the following code. Now, I would like to store the field names as strings in a variable. How can I do this?
Desired output:
var = ("Field 1", "Field2", "Field.3")
Another try (does not work either):
for field in fields:
var = str(field.name)
Current code:
fields = arcpy.ListFields('df')
# Show column names
for field in fields:
print(field.name)
Solved! Go to Solution.
I've taken what you asked for quite literally (i.e. replaced single speech-marks with doubles and square brackets with curly) hence the multiple replace operations on the final variable but I think it should achieve what you're after
fields = arcpy.ListFields('df')
fieldlist = []
for field in fields:
fieldlist.append(field.name)
var = str(fieldlist).replace("'", '"').replace("[", "(").replace("]", ")")
print var
The major issue you are having is that arcpy.ListFields returns just that, fields or field objects, and not field names.
var = [field.name for field in arcpy.ListFields('df')]
I've taken what you asked for quite literally (i.e. replaced single speech-marks with doubles and square brackets with curly) hence the multiple replace operations on the final variable but I think it should achieve what you're after
fields = arcpy.ListFields('df')
fieldlist = []
for field in fields:
fieldlist.append(field.name)
var = str(fieldlist).replace("'", '"').replace("[", "(").replace("]", ")")
print var
The major issue you are having is that arcpy.ListFields returns just that, fields or field objects, and not field names.
var = [field.name for field in arcpy.ListFields('df')]
Hi Joshua, I might ping you, if you have sometime, since now I am trying to combine the code in your reply with the code you helped with earlier in the split comma separated values post. Thank you!