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],
}
)
你可以張貼屬性錯誤。如果exampleData是一個列表,那麼它沒有一個屬性行。 –
正如@GarrettR所說,列表沒有行屬性。你可以這樣做:'用於範圍內的行(1,len(exampleData)):' – bernie
當然,然後無處不在他有exampleData(row)需要改爲exampleData [row]。但我認爲這個問題比這個更重要。你能發佈幾行'zdfile-test.csv'嗎? –