【问题标题】:ValueError: two fields with the same nameValueError:两个具有相同名称的字段
【发布时间】:2015-03-12 02:13:18
【问题描述】:

我有一个脚本,它使用 NumPy 元组将 JSON Web 服务写入 Esri 文件地理数据库。我收到了错误,ValueError: two fields with the same name。我的数据没有表明这一点。我的脚本在下面。我正在使用 Python 2.7。这可能是什么原因?

import json
import jsonpickle
import requests
import arcpy
import numpy as np    #NOTE THIS

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






f2 = open('C:\Users\Administrator\Desktop\DetailView.json', 'r')
data2 = jsonpickle.encode( jsonpickle.decode(f2.read()) )

url2 = "myURL"
headers2 = {'Content-type': 'text/plain', 'Accept': '/'}

r2 = requests.post(url2, data=data2, headers=headers2)
decoded2 = json.loads(r2.text)


dt = np.dtype([('SRAddress', 'U40'),
                ('Latitude', '<f8'),
                ('Longitude', '<f8'),
                ('Type', 'U40'),
                ('SRNumber', 'U40'),
                ('FirstName', 'U40'),
               ('LastName', 'U40'),
               ('HomePhone', 'U40'),
               ('Comment', 'U128'),
                ('ItemInfo', 'U128'),
                ('Day', 'U128'),
                ('DistrictName', 'U128'),
                ('ShortDay', 'U128'),
                ('Name', 'U128'),
                ('A_Call_No','U128'),
                ('Area', 'U128'),
                ('DirectionSuffix','U128'),
                ('DistrictAbbr', 'U128'),
                ('DistrictNumber', 'U128'),
                ('DistrictOffice', 'U128'),
                ('Fraction', 'U128'),
                ('R_Call_No', 'U128'),
                ('SectionId', 'U128'),
                ('StreetFrom', 'U128'),
                ('StreetTo', 'U128'),
                ('StreetFrom', 'U128'),
                ('StreetLightId', 'U128'),
                ('StreetLightStatus', 'U128'),
                ('Y_Call_No', 'U128'),
                ('CommunityPlanningArea', 'U128'),
                ('LastUpdatedBy', 'U128'),
                    # ('BOSRadioHolderName', 'U128'),
                    ])









items = []
for sr in decoded2['Response']['ListOfServiceRequest']['ServiceRequest']:
    SRAddress = sr['SRAddress']
    Latitude = sr['Latitude']
    Longitude = sr['Longitude']
    SRNumber = sr['SRNumber']
    FirstName = sr['FirstName']
    LastName = sr['LastName']
    HomePhone = sr['HomePhone']

    ItemInfo = " "

    for ew in sr["ListOfLa311ElectronicWaste"][u"La311ElectronicWaste"]:
            CommodityType = ew['Type']
            ItemType = ew['ElectronicWestType']
            ItemCount = ew['ItemCount']
            ItemInfo += '{0},  {1}, '.format(ItemType, ItemCount)
            ParentNumber = ew['Name']




    for GIS in sr["ListOfLa311GisLayer"][u"La311GisLayer"]:
            Day = GIS['Day']
            DistrictName = GIS['DistrictName']
            ShortDay = GIS['ShortDay']
            A_Call_No = GIS['A_Call_No']
            Area = GIS['Area']
            DirectionSuffix = GIS['DirectionSuffix']
            DistrictAbbr = GIS['DistrictAbbr']
            DistrictNumber = GIS['DistrictNumber']
            DistrictOffice = GIS['DistrictOffice']
            Fraction = GIS['Fraction']
            R_Call_No = GIS['R_Call_No']
            SectionId = GIS['SectionId']
            StreetFrom = GIS ['StreetFrom']
            StreetTo = GIS ['StreetTo']
            StreetLightId = GIS ['StreetLightId']
            StreetLightStatus = GIS['StreetLightStatus']
            Y_Call_No = GIS ['Y_Call_No']
            CommunityPlanningArea = GIS['CommunityPlanningArea']
            LastUpdatedBy = GIS['LastUpdatedBy']
            BOSRadioHolderName = GIS['BOSRadioHolderName']






    comments =  [ cl['Comment'] for cl in sr["ListOfLa311ServiceRequestNotes"][u"La311ServiceRequestNotes"]]
    print comments
    Comment = ' '.join(comments)


items.append((SRAddress,
                          Latitude,
                          Longitude,
                          CommodityType,
                          SRNumber,
                         FirstName,
                          LastName,
                          HomePhone,
                          Comment,
                          ItemInfo,
                          Day,
                          DistrictName,
                          ShortDay,
                          ParentNumber,
                         A_Call_No,
                        Area,
                        DirectionSuffix,
                        DistrictAbbr,
                        DistrictNumber,
                        DistrictOffice,
                        Fraction,
                        R_Call_No,
                        SectionId,
                        StreetFrom,
                        StreetTo,
                        StreetLightId,
                        StreetLightStatus,
                        Y_Call_No,
                        CommunityPlanningArea,
                        LastUpdatedBy,
                        BOSRadioHolderName
))


arr = np.array(items,dtype=dt)
sr = arcpy.SpatialReference(4326)
arcpy.da.NumPyArrayToFeatureClass(arr, fc, ['longitude', 'latitude'], sr )

arcpy.AddField_management(fc, 'CYLA_COMMUNITY', "TEXT", 255)
arcpy.AddField_management(fc, 'CYLA_DISTRICT', "TEXT", 255)
arcpy.AddField_management(fc, 'CYLA_SERVICE_DAY', "TEXT", 255)
arcpy.AddField_management(fc, 'X_COR', "FLOAT", 255)
arcpy.AddField_management(fc, 'Y_COR', "FLOAT", 255)



print json.dumps(decoded2, sort_keys=True, indent=4)

【问题讨论】:

  • 完整的错误跟踪并在仍然收到错误的同时尽可能压缩代码会有所帮助。

标签: python json numpy


【解决方案1】:

'StreetFrom' 在您的自定义 dtype 中提供了两次。

...
('StreetFrom', 'U128'),
('StreetTo', 'U128'),
('StreetFrom', 'U128'),
('StreetLightId', 'U128'),
...

【讨论】:

  • 这是正确的,多一双眼睛来看看你的代码总是很好的。
猜你喜欢
  • 2022-06-12
  • 1970-01-01
  • 2016-08-25
  • 1970-01-01
  • 1970-01-01
  • 2022-09-30
  • 2022-01-09
  • 2022-12-05
  • 2016-01-01
相关资源
最近更新 更多