Python csv读取器范围上的属性错误
我有以下代码,它应该遍历我的csv文件并创建json输出。但是,我在exampledata对象上遇到一个属性错误,即不存在行或行属性。我知道我可以通过[0][0]指定特定的行和列,但需要在for循环中动态调用该行,而不是专门调用它。我能做些什么来解决这个问题,是否有一个特定的“行”或索引可以替代,即:exampledata[I][0]Python csv读取器范围上的属性错误,python,csv,Python,Csv,我有以下代码,它应该遍历我的csv文件并创建json输出。但是,我在exampledata对象上遇到一个属性错误,即不存在行或行属性。我知道我可以通过[0][0]指定特定的行和列,但需要在for循环中动态调用该行,而不是专门调用它。我能做些什么来解决这个问题,是否有一个特定的“行”或索引可以替代,即:exampledata[I][0] payloads = [] users_dict = {'users': []} exampleFile = open('zdfile-t
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],
}
)
正如@GarrettR所说,列表没有row属性。您可以改为:
for row in range(1, len(exampleData)):
# ^^^
在下一行中还将出现错误,因此您可以执行以下操作:
if exampleData[row][2]:
# ^ ^
你能发布属性错误吗。如果exampleData是一个列表,那么它没有属性行。正如@GarrettR所说,列表没有行属性。您可以改为:
对于范围内的行(1,len(exampleData)):
当然,那么他得到exampleData(row)的任何地方都需要更改为exampleData[row]。但我认为问题更为根本。您可以发布几行zdfile test.csv
?在下一行中也会出现错误。改为:exampleData[row][2]
Bernie,这很有效,谢谢!Garrett,错误是:对于范围内的行(1,exampleData.row):AttributeError:'list'对象没有属性'row'