TypeError:需要一个类似字节的对象,而不是python和CSV中的'str' [英] TypeError: a bytes-like object is required, not 'str' in python and CSV

查看:2280
本文介绍了TypeError:需要一个类似字节的对象,而不是python和CSV中的'str'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


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


而执行下面的python代码将HTML表格数据保存在Csv文件中。不知道如何获取rideup.pls帮助我。

  import csv 
import requests
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 table.findAll('tr')[1:]:
list_of_cells = []
在row.findAll('td')中的单元格:
list_of_cells.append(cell.text)
list_of_rows.append(list_of_cells)
outfile = open('./ immates.csv','wb')
writer = csv。作者(outfile)
writer.writerow([SNo,States,Dist,Population])
writer.writerows(list_of_rows)

解决方案

您使用的是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中采用二进制模式。 / p>

已编辑为





  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 = []
用于row.findAll('td')中的单元格:
list_of_cells.append(cell.text)
list_of_rows.append(list_of_cells)
outfile = open('./ immates.csv','w')
writer = csv.writer
writer.writerow(['SNo','States','Dist','Population'])
writer.writerows(list_of_rows)


TypeError: a bytes-like object is required, not 'str'

getting above error while Executing below python code to save the HTML table data in Csv file. don't know how to get rideup.pls help me.

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)

on above the last line.

解决方案

You are using Python 2 methodology instead of Python 3.

Change:

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

To:

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

and you will get a file with the following output:

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
.....

In Python 3 csv takes the input in text mode, whereas in Python 2 it took it in binary mode.

Edited to Add

Here is the code I ran:

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)

这篇关于TypeError:需要一个类似字节的对象,而不是python和CSV中的'str'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆