this is a follow up of this post. To reiterate, I have text file with metadata and measurement data of a given location. I want to write this data to spatial mapping software called ArcGIS for which I have to specify for each value in the list the data type as defined in ArcGIS. For example:

type("foo") 

Gives str in Python, but is known as text in ArcGIS. So, I want to translate the datatype of every element in my list, to the appropriate datatype in ArcGIS. Something like this:

# This is the raw data that I want to write to ArcGIS
foo= ['plot001', '01-01-2013', 'XX', '10', '12.5', '0.65', 'A']
# The appropriate datatypes in Python are:
dt= [str, str, str, int, float, float, str]
# In ArcGIS, the datatypes should be:
dtArcGIS= ['text', 'text', 'text', 'short', 'float', 'float', 'text']

The question is: how can I come from dt to dtArcGIS? I was thinking of a dictionary as:

dtDict= dict{str:'text', int:'short', float:'float'}

But this gives a syntax error. Any help would be appreciated, thanks!

有帮助吗?

解决方案

You are mixing two formats, just remove the dict like this

dtDict = {str:'text', int:'short', float:'float'}

And this is how you should convert the types

foo = ['plot001', '01-01-2013', 'XX', '10', '12.5', '0.65', 'A']
from ast import literal_eval

dt = []
for item in foo:
    try:
        dt.append(type(literal_eval(item)))
    except:
        dt.append(str)

dtDict = {str:'text', int:'short', float:'float'}
print map(dtDict.get, dt)

Output

['text', 'text', 'text', 'short', 'float', 'float', 'text']
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top