Python Forum

Full Version: ARCPY enter excel table into coded domain
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
This is what i have been trying so far, i cant seem to get the dictionary in the right form.
Appreciate any help i can get

>>> import arcpy
>>> from arcpy import env
>>> env.workspace = 'C:\Milgeo\python'
>>> arcpy.CreateDomain_management('presidentos.gdb', 'amerikanske presidenter', 'amerikas presidenter fra Washington til Trump', "SHORT", "CODED")
<Result 'C:\\Milgeo\\python\\presidentos.gdb'>
>>> import pandas
>>> import pandas as pd
>>> file_path = 'C:\Milgeo\python\president.xlsx'
>>> df = pd.read_excel(file_path, encoding = 'utf-16')
>>> df.to_dict()
{u'Presidenter': {0: u'George Washington', 1: u'John Adams', 2: u'Thomas Jefferson', 3: u'James Madison', 4: u'James Monroe', 5: u'John Quincy Adams', 6: u'Andrew Jackson', 7: u'Martin Van Buren', 8: u'William H. Harrison', 9: u'John Tyler', 10: u'James Knox Polk', 11: u'Zachary Taylor', 12: u'Millard Fillmore', 13: u'Franklin Pierce', 14: u'James Buchanan', 15: u'Abraham Lincoln', 16: u'Andrew Johnson', 17: u'Ulysses S. Grant', 18: u'Rutherford B. Hayes', 19: u'James Garfield', 20: u'Chester A. Arthur', 21: u'Grover Cleveland', 22: u'Benjamin Harrison', 23: u'Grover Cleveland', 24: u'William McKinley', 25: u'Theodore Roosevelt', 26: u'William H. Taft', 27: u'Woodrow Wilson', 28: u'Warren G. Harding', 29: u'Calvin Coolidge', 30: u'Herbert Hoover', 31: u'Franklin D. Roosevelt', 32: u'Harry S. Truman', 33: u'Dwight D. Eisenhower', 34: u'John F. Kennedy', 35: u'Lyndon B. Johnson', 36: u'Richard Nixon', 37: u'Gerald Ford', 38: u'Jimmy Carter', 39: u'Ronald Reagan', 40: u'George H.W. Bush', 41: u'Bill Clinton', 42: u'George W. Bush', 43: u'Barack Obama', 44: u'Donald J. Trump'}}
>>> for code in df.to_dict():
...     arcpy.AddCodedValueToDomain_management('presidentos.gdb', "amerikanske presidenter", 'code', 'df.to_dict[code]')
...
Error:
Runtime error Traceback (most recent call last): File "<string>", line 2, in <module> File "c:\program files (x86)\arcgis\desktop10.6\arcpy\arcpy\management.py", line 1492, in AddCodedValueToDomain raise e ExecuteError: ERROR 000378: Invalid Short type. >>>
arcpy.AddCodedValueToDomain_management('presidentos.gdb', "amerikanske presidenter", 'code', 'df.to_dict[code]')
should be
arcpy.AddCodedValueToDomain_management('presidentos.gdb', "amerikanske presidenter", code, df.to_dict[code])
By the way, this looks like a lot of typing in interactive mode. Why not save it as py file and run the script? So you will be able to reuse it whenever you need
I tried this and got the following error:
Error:
Runtime error Traceback (most recent call last): File "<string>", line 2, in <module> TypeError: 'instancemethod' object has no attribute '__getitem__'
Sorry, I miss the parenthesis after to_dict, i.e. the last argument should be
df.to_dict()[code]
(Feb-22-2019, 08:41 PM)buran Wrote: [ -> ]By the way, this looks like a lot of typing in interactive mode. Why not save it as py file and run the script? So you will be able to reuse it whenever you need
By the way i just started using python to simplify some of my work. And i have no education on it what so ever, so I really appreciate any kind of help!
take a look at
https://python-forum.io/Thread-How-to-Ex...ython-code

You can write your code in any texteditor (e.g. Notepad, but not Word). However better look at some real IDE, that will have added benefits like code highlighting, code completion, debugger, linter, etc. I would recommned VS Code.