qq_39411183
qq_39411183
采纳率0%
2019-04-02 17:13

Python爬虫代码除了点问题,写入到csv文件出错,rows类型为list,求教各位大佬?

5

将数据存入csv文件时报错,然后各种查,就是解决不了,rows类型为list

部分源码:
with open(city+'.csv','a') as f:

fieldnames = ['name','area','address','total_price','unit_price','house_size','house_type']
writer = csv.DictWriter(f, fieldnames=fieldnames)
writer.writeheader()
46 rows = [name,area,address,total_price,unit_price,house_size,house_type,number]
47 print(type(rows))
48 writer.writerow(rows)

报错信息:
Traceback (most recent call last):
File "/home/tom/PycharmProjects/spider1/spider1.py", line 48, in
writer.writerow(rows)
File "/usr/lib/python3.6/csv.py", line 155, in writerow
return self.writer.writerow(self._dict_to_list(rowdict))
File "/usr/lib/python3.6/csv.py", line 148, in _dict_to_list
wrong_fields = rowdict.keys() - self.fieldnames
AttributeError: 'list' object has no attribute 'keys'

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

为你推荐