值错误:无法将字符串转换为float,NumPy

2024-10-01 17:23:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个脚本,其中我正在向Esri文件geodatabase写入一个JSON web服务。我收到错误值错误:无法将字符串转换为浮点:微波

我以前使用过完全相同的脚本,U40是所有字符串的数据类型。在

我的脚本和结果如下

import json
import jsonpickle
import requests
import arcpy
import numpy

fc = "C:\MYLATesting.gdb\MYLA311"
if arcpy.Exists(fc):
  arcpy.Delete_management(fc)

f = open('C:\Users\Administrator\Desktop\myla311.json', 'r')

data = jsonpickle.encode( jsonpickle.decode(f.read()) )

url = "myUrl"
headers = {'Content-type': 'text/plain', 'Accept': '/'}

r = requests.post(url, data=data, headers=headers)
sr = arcpy.SpatialReference(4326)

decoded = json.loads(r.text)

SRAddress = decoded['Response']['ListOfServiceRequest']['ServiceRequest'][0]['SRAddress']
latitude = decoded['Response']['ListOfServiceRequest']['ServiceRequest'][0]['Latitude']
longitude = decoded['Response']['ListOfServiceRequest']['ServiceRequest'][0]['Longitude']

CommodityType = decoded['Response']['ListOfServiceRequest']['ServiceRequest'][0]['ListOfLa311ElectronicWaste']['La311ElectronicWaste'][0]['Type']
ItemType = decoded['Response']['ListOfServiceRequest']['ServiceRequest'][0]['ListOfLa311ElectronicWaste']['La311ElectronicWaste'][0]['ElectronicWestType']
ItemCount = decoded['Response']['ListOfServiceRequest']['ServiceRequest'][0]['ListOfLa311ElectronicWaste']['La311ElectronicWaste'][0]['ItemCount']


CommodityType1 = decoded['Response']['ListOfServiceRequest']['ServiceRequest'][0]['ListOfLa311ElectronicWaste']['La311ElectronicWaste'][1]['Type']
ItemType1 = decoded['Response']['ListOfServiceRequest']['ServiceRequest'][0]['ListOfLa311ElectronicWaste']['La311ElectronicWaste'][1]['ElectronicWestType']
ItemCount1 = decoded['Response']['ListOfServiceRequest']['ServiceRequest'][0]['ListOfLa311ElectronicWaste']['La311ElectronicWaste'][1]['ItemCount']






print SRAddress
print latitude
print longitude

print CommodityType
print ItemType
print ItemCount






print CommodityType1
print ItemType1
print ItemCount1



item ={'SRAddress': SRAddress, 'Longitude': longitude, 'Latitude': latitude, 'CommodityType': CommodityType, 'ItemType': ItemType, 'ItemCount': ItemCount}
import numpy as np     #NOTE THIS
keys = ['SRAddress','Longitude','Latitude','CommodityType','ItemType', 'ItemCount']
k1,k2,k3, k4, k5, k6 = keys
data_line ={'SRAddress': SRAddress, 'Longitude': longitude, 'Latitude': latitude, 'CommodityType': CommodityType, 'ItemType': ItemType, 'ItemCount': ItemCount}
frmt = '\nStraight dictionary output\n Address: {} Long: {} Lat: {}'
print(frmt.format(item[k1],item[k2],item[k3], item[k4],item[k5], item[k6]))
print '\noption 1:  List comprehension with unicode'
a =  tuple([unicode(item[key]) for key in keys])  # list comprehension with unicode
print('{}'.format(a))
dt = np.dtype([('SRAddress','U40'),('CommodityType','U40'), ('ItemType','U40'), ('ItemCount','U40'),('longitude','<f8'),('latitude','<f8')])
arr = np.array(a,dtype=dt)
print'\narray unicode\n',arr
print'dtype',arr.dtype
print '\noption 2:List comprehension without unicode'
b = tuple([item[key] for key in keys])
print('{}'.format(b))
dt = np.dtype([('SRAddress','U40'),('CommodityType','U40'), ('ItemType','U40'), ('ItemCount','U40'),('longitude','<f8'),('latitude','<f8')])
arr = np.array(b,dtype=dt)
print'\narray without unicode\n',arr
print'dtype',arr.dtype

arcpy.da.NumPyArrayToFeatureClass(arr, fc, ['longitude', 'latitude'], sr)

结果

^{pr2}$

Tags: importresponseitemdecodedprintarrdtypelatitude
1条回答
网友
1楼 · 发布于 2024-10-01 17:23:32

你有

keys = ['SRAddress','Longitude','Latitude','CommodityType','ItemType', 'ItemCount']

然后,脚本将使用这些键按顺序从dict items生成一个值的元组:

^{pr2}$

然后当你把这个元组转换成数组时

arr = np.array(a,dtype=dt)

它试图将与ItemType关联的值填充到结构的longitude字段中。{cd4}字段的顺序应与dtype的顺序相同。理想情况下,您甚至不需要复制这些信息,而是使用dt.names。那么只要struct字段的名称与您试图转换的dict相同,它就应该以正确的顺序获取值。在

相关问题 更多 >

    热门问题