2016-03-04 50 views
1

我有以下代码应该迭代通过我的csv文件并创建我的json输出。但是,我在我的exampledata对象上收到一个属性错误,它没有行或行属性存在。我知道我可以通过[0] [0]指定特定的行和列,但需要动态调用for循环中的行,并专门调用它。我能做些什么来解决这个问题,是否有一个特定的“行”或索引,我可以使用,即:exampledata [i] [0]?csv阅读器范围的属性错误

payloads = [] 
    users_dict = {'users': []} 
    exampleFile = open('zdfile-test.csv') 
    exampleReader = csv.reader(exampleFile) 
    exampleData = list(exampleReader) 

    for row in range(1, exampleData.row): 
     if exampleData(row)[2]: 
     users_dict['users'].append(
      { 
      "name": exampleData(row)[0], 
      "email": exampleData(row)[2], 
      "external_id": exampleData(row)[3], 
      "details": exampleData(row)[4], 
      "notes": exampleData(row)[5], 
      "phone": exampleData(row)[6], 
      "role": exampleData(row)[7], 
      "organization_id": "", 
      "tags": exampleData(row)[10], 
      "password": exampleData(row)[11], 
      "user_fields": {"nickname": exampleData(row)[1],"employee_phone_number": exampleData(row)[12],"employee_id": exampleData(row)[13],"employee_title": exampleData(row)[14],"employee_department": exampleData(row)[15],"employee_manager": exampleData(row)[16],"associate_id": exampleData(row)[17],"office_status": exampleData(row)[18],"customer_class": exampleData(row)[19],"primary_title": exampleData(row)[20],"associate_status": exampleData(row)[21],"joined_date": exampleData(row)[22],"e_mail": exampleData(row)[23],"isp_e_mail": exampleData(row)[24],"mobile": exampleData(row)[25],"office_name": exampleData(row)[26],"office_id": exampleData(row)[27],"office_city": exampleData(row)[28],"office_state": exampleData(row)[29],"office_phone": exampleData(row)[30],"region": exampleData(row)[31],"region_id": exampleData(row)[32],"region_type": exampleData(row)[33]} 
     }, 
     { 
      "external_id": exampleData(row)[3], 
      "email": exampleData(row)[23], 
     } 
    ) 
+0

你可以张贴属性错误。如果exam​​pleData是一个列表,那么它没有一个属性行。 –

+0

正如@GarrettR所说,列表没有行属性。你可以这样做:'用于范围内的行(1,len(exampleData)):' – bernie

+0

当然,然后无处不在他有exampleData(row)需要改为exampleData [row]。但我认为这个问题比这个更重要。你能发布几行'zdfile-test.csv'吗? –

回答

1

正如@GarrettR所说,列表没有行属性。你可以这样做,而不是:

for row in range(1, len(exampleData)): 
    #    ^^^ 

在下面的行也将是一个错误,所以你可以这样做:

if exampleData[row][2]: 
#   ^^