I'd like to create a list of tuples where each tuple contains the data from the fields of one feature class: the objective is to iterate through the list and add fields of the name, type and length provided in each of the tuples to a different feature class. I've hit a snag and I'm sure how to un-snag myself:
fc = r'C:\path\to_gdb\featureClass'
fieldList = []
for f in arcpy.ListFields(fc):
x = (f'("{f.name}","{f.type}",{f.length})')
fieldList.append(x)
works pretty good, but not quite perfect. Each of the tuples is bounded by single quotes so if I try to use the tuple indexes to extract a tuple element, things head south:
fieldList
Out[79]:
['("OBJECTID","OID",4)',
'("SHAPE","Geometry",0)',
'("siteaddid","String",20)',
'("addressptid","String",20)',
'("rclnguid","String",254)',
'("discrpagid","String",75)',
'("preaddrnum","String",5)',
'("addrnumsuf","String",5)',
...]
I can cheat (and I have) and just copied and pasted fieldList to notepad and deleted the single quotes, but that's using a hammer; how would I create a clean list of tuples without the bounding single quotes:
cleanList
Out[82]:
[('OBJECTID', 'OID', 4),
('SHAPE', 'Geometry', 0),
('siteaddid', 'String', 20),
('addressptid', 'String', 20),
('rclnguid', 'String', 254),
('discrpagid', 'String', 75),
('preaddrnum', 'String', 5),
('addrnumsuf', 'String', 5),
...]
Solved! Go to Solution.
Why are you building a string representation of a tuple:
x = (f'("{f.name}","{f.type}",{f.length})')
instead of just building a tuple:
x = (f.name,f.type,f.length)
Why are you building a string representation of a tuple:
x = (f'("{f.name}","{f.type}",{f.length})')
instead of just building a tuple:
x = (f.name,f.type,f.length)
F (orget) the f-strings. Remember they are always going to produce a string output.
Typical Joe blunder. That's why I needed a couple extra set of eyes....