使用字典将p​​ython数据类型转换为其他语言的数据类型

时间:2013-12-21 11:55:36

标签: python dictionary arcgis type-conversion arcpy

这是this post的后续行动。重申一下,我的文本文件包含给定位置的元数据和测量数据。我想将此数据写入名为ArcGIS的空间映射软件,我必须为列表中的每个值指定ArcGIS 中定义的数据类型。例如:

type("foo") 

在Python中提供str,但在ArcGIS中称为text。因此,我想将列表中每个元素的数据类型转换为ArcGIS中的相应数据类型。像这样:

# 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']

问题是:我如何从dtdtArcGIS ?我想的是dictionary

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

但这会产生语法错误。任何帮助将不胜感激,谢谢!

1 个答案:

答案 0 :(得分:1)

您正在混合使用两种格式,只需删除此类dict

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

这就是你应该如何转换类型

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)

<强>输出

['text', 'text', 'text', 'short', 'float', 'float', 'text']