python - 值错误 : could not convert string to float, NumPy

标签 python numpy

我有一个脚本,正在将 JSON Web 服务写入 Esri 文件地理数据库。我收到错误 ValueError: 无法将字符串转换为 float : 微波炉

我之前使用过完全相同的脚本,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)

结果

C:\Python27\ArcGIS10.2\python.exe C:/MYLAScripts/MYLAJson.py
Traceback (most recent call last):
5810 N WILLIS AVE, 91411
  File "C:/MYLAScripts/MYLAJson.py", line 71, in <module>
34.176277
    arr = np.array(a,dtype=dt)
-118.455249
ValueError: could not convert string to float: Microwaves
Electronic Waste
Microwaves
3
Electronic Waste
Televisions (Any Size)
6

Straight dictionary output
 Address: 5810 N WILLIS AVE, 91411 Long: -118.455249 Lat: 34.176277

option 1:  List comprehension with unicode
(u'5810 N WILLIS AVE, 91411', u'-118.455249', u'34.176277', u'Electronic Waste', u'Microwaves', u'3')

最佳答案

你有

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

然后脚本按顺序使用这些键从字典 items 中创建一个值元组:

a =  tuple([unicode(item[key]) for key in keys])

然后当你将此元组转换为数组时

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

它试图将与 ItemType 关联的值填充到结构的 longitude 字段中。 keys 列表的顺序应与数据类型的结构字段的顺序相同。理想情况下,您甚至不需要复制这些信息,而是使用 dt.names。然后,只要结构字段与您尝试转换的字典具有相同的名称,它就应该以正确的顺序获取值。

关于python - 值错误 : could not convert string to float, NumPy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28595465/

相关文章:

python - 为什么python程序第一次运行很慢?

python strptime 忽略 %A?

python - 具有转换的重复数组

python - 混合类型的numpy数据切片

python - 将字符串中的数字转换为python中的数组

python - 如何将年龄和性别数据添加到卷积神经网络的图像中?

python - 有没有办法返回方法文档字符串?

python - 仅使用数字 `matrix[:,mask]` 和 `matrix[mask,:]` 将 `0` 转置为 `1` ?

python - 为不同的 ndarray 索引分配相同的值 - Python

python - Boost Python,Visual Studio 不创建 DLL