0

In the code below, the line 'alias = dict[fld.name]' always throws an exception. I don't see why; printing out fld.name works just fine. Also, the exception message is the field name printing correctly.

Printing the dictionary keys and values also works fine. The ultimate goal is to get a string to set as the field alias using AlterField.

The dictionary seems to be built ok. I can print out key/value pairs with iteritems(). The problem is in accessing the values, using field names returned by arcpy.ListFields, which also print on on their own. I've tried setting the key (fld.name) to a variable, enclosing it in quotes, to no avail. I keep getting an error in the form 'KeyError: u'B01001m1'. B01001m1 is a valid key.

with arcpy.da.SearchCursor(metatable, metaflds) as rows:
        for row in rows:
            if not moe in row[1]:
                fn = row[1].replace(' ', '')
                fn = fn.replace(est, '')
                dict[row[0]] = fn

    # Get list of tables to add alias to
    tbls = arcpy.ListTables()
    for tbl in tbls:
        tblflds = arcpy.ListFields(tbl)
        for fld in tblflds:
            try:
                alias = dict[fld.name] # exception here
                print(alias)
            except Exception as e:
                import traceback
                import sys
                tb = sys.exc_info()[2]
                print('Oh no!')
                print("Line {0}".format(tb.tb_lineno))
                print(e.message)
11
  • A Python dictionary has keys and values:d = dict(one=2, two=3) or d = {'one': 2, 'two': 3} but no d= dict(one) only – gene Dec 8 '14 at 17:32
  • I might be misunderstanding, but the dictionary is built ok. Printing it's keys and values with iteritems() works fine. The line throwing the exception is trying to access the value that should be associated with dict[fld.name] (a plain dict[fld] throws an exception because fld is a field object). In your example, dict['one'] should return 2. Note I'm accessing the dict with square brackets, not curved. – recurvata Dec 8 '14 at 17:39
  • The brackets are for accessing the value of a key print d['one']gives 2. What is the content of fld.name ? – gene Dec 8 '14 at 17:46
  • fld.name = the name of the fields in the current table in the for loop iterating through the tblfields list. Thses are census tables, so fld.name = names like B23009e11, B01001m3, and so on. Since these are decidedly non-intuitive, dict consists of these field names as keys and a more user friendly description as values. I want to use these values for an alias field (some are too long to use as field names). So dict[fld.name] should return the value for, i.e, B23009e11, which might be 'Age:25-44', for example. – recurvata Dec 8 '14 at 17:53
  • ok, if I define a dictionary with d= dict(B23009e11='Age:25-44')the correct result is print dict['B23009e11'] and not print dict[[B23009e11]because B23009e11 is a variable non defined. You cannot make a dictionary with d = {B23009e11:'Age:25-44'}(you can try in a Python Shell) – gene Dec 8 '14 at 18:20
1

Well, I got the script to work by changing alias = dictflds[name] to alias = dictflds.get(fld.name, 'None'). I'd still like to know why the first method didn't work, but that's a question for a day when I don't have any other projects. Which is to say I may never know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.