栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

TypeError:需要一个类似字节的对象,而在python和CSV中不是'str'

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

TypeError:需要一个类似字节的对象,而在python和CSV中不是'str'

您正在使用Python 2方法而不是Python 3。

更改:

outfile=open('./immates.csv','wb')

至:

outfile=open('./immates.csv','w')

您将获得一个带有以下输出的文件:

SNo,States,Dist,Population1,Andhra Pradesh,13,493787762,Arunachal Pradesh,16,13826113,Assam,27,311692724,Bihar,38,1038046375,Chhattisgarh,19,255401966,Goa,2,14577237,Gujarat,26,60383628.....

在Python 3中,csv以文本模式获取输入,而在Python 2中,csv以二进制模式获取输入。

编辑添加

这是我运行的代码:

url='http://www.mapsofindia.com/districts-india/'html = urllib.request.urlopen(url).read()soup = BeautifulSoup(html)table=soup.find('table', attrs={'class':'tableizer-table'})list_of_rows=[]for row in table.findAll('tr')[1:]:    list_of_cells=[]    for cell in row.findAll('td'):        list_of_cells.append(cell.text)    list_of_rows.append(list_of_cells)outfile = open('./immates.csv','w')writer=csv.writer(outfile)writer.writerow(['SNo', 'States', 'Dist', 'Population'])writer.writerows(list_of_rows)


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/647877.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号