【问题标题】:Translate python datatype to datatype of other language, using a dictionary使用字典将 python 数据类型转换为其他语言的数据类型
【发布时间】:2014-01-10 04:57:50
【问题描述】:

这是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'}

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

【问题讨论】:

  • 去掉dict,花括号已经把它定义为一个dict,不需要告诉Python再做一次,dtDict= {str:'text', int:'short', float:'float'}
  • type('10')str,而不是 int
  • 感谢您指出这一点!

标签: python dictionary arcgis type-conversion arcpy


【解决方案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']

【讨论】:

    猜你喜欢
    • 2021-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-13
    • 2022-01-06
    • 1970-01-01
    • 2017-05-30
    • 1970-01-01
    相关资源
    最近更新 更多