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

ShivaGuntuku:

TypeError:需要一个类似字节的对象,而不是'str'

在执行以下python代码以将HTML表格数据保存到Csv文件时遇到上述错误。不知道如何获得rideup.pls帮助我。

import csv
import requests
from bs4 import BeautifulSoup

url='http://www.mapsofindia.com/districts-india/'
response=requests.get(url)
html=response.content

soup=BeautifulSoup(html,'html.parser')
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','wb')
writer=csv.writer(outfile)
writer.writerow(["SNo", "States", "Dist", "Population"])
writer.writerows(list_of_rows)

在最后一行上方。

dstudeba:

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

更改:

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

至:

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

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

SNo,States,Dist,Population
1,Andhra Pradesh,13,49378776
2,Arunachal Pradesh,16,1382611
3,Assam,27,31169272
4,Bihar,38,103804637
5,Chhattisgarh,19,25540196
6,Goa,2,1457723
7,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)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

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

TypeError:需要一个类似字节的对象,而在尝试写入csv文件时不是'str'

TypeError:需要一个类似字节的对象,而在Python 3中打开Python 2 Pickle文件时不是'str'

使用Python 3.5,抛出错误:TypeError:需要一个类似字节的对象,而不是'str'

Python 3-TypeError:需要一个类似字节的对象,而不是'str'

Python 2到3“ TypeError:需要一个类似字节的对象,而不是'str'”

TypeError:需要一个类似字节的对象,而不是'str'–用Python保存JSON数据

Python 3.7 TypeError:需要一个类似字节的对象,而不是'str'

python 3.5:“ TypeError:memoryview:需要一个类似字节的对象,而不是'str'”

Python3 TypeError:需要一个类似字节的对象,而不是“str”

Python3 .replace产生TypeError:需要一个类似字节的对象,而不是'str'

Python 3,TypeError:需要一个类似字节的对象,而不是'str'

Python错误:TypeError:需要一个类似字节的对象,而不是'str'

TypeError:需要一个类似字节的对象,而不是从文件中读取时的“ str”

TypeError:需要一个类似字节的对象,而不是subprocess.check_output中的“ str”

TypeError:在Python3中写入文件时需要一个类似字节的对象,而不是'str'

Python 3.6:TypeError:在尝试打印页面中的所有链接时,需要一个类似字节的对象,而不是'str'

TypeError:在Python 3.5.1中使用REST时需要一个类似字节的对象,而不是'str'

需要一个类似字节的对象,而不是python中的“ str”错误

需要一个类似字节的对象,而不是'str'

错误:需要一个类似字节的对象,而不是'str'

TypeError:需要一个类似字节的对象,而不是'str''xxx';'xxx'

如何解决Json错误:TypeError:需要一个类似字节的对象,而不是'str'

子进程“ TypeError:需要一个类似字节的对象,而不是'str'”

TypeError:需要一个类似字节的对象,而不是由拆分函数引起的'str'

TypeError:需要一个类似字节的对象,而不是'str'-HMAC

TypeError:在控制台中输入命令时,需要一个类似字节的对象,而不是“ str”

Django Rest Framework TypeError需要一个类似字节的对象,而不是'str'

Python 3.5 TypeError:需要一个类似字节的对象,而不是使用truncate过滤器的'str'