TypeError: a byte object is needed, not 'str' in python and CSV

TypeError: requires an object with a byte name, not 'str'

error exceeded when executing below python code to save HTML table data in a 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) 

above the last line.

+135
html-table csv beautifulsoup
Dec 15 '15 at 7:20
source share
4 answers

Instead of Python 3, you use the Python 2 methodology.

Edit:

 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 input in text mode, while in Python 2 it takes 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) 
+258
Dec 15 '15 at 8:12
source share

I had the same issue with Python3. My code was written in io.BytesIO() .

Replacing io.StringIO() resolved.

+16
Dec 22 '16 at 21:20
source share
 file = open('parsed_data.txt', 'w') for link in soup.findAll('a', attrs={'href': re.compile("^http")}): print (link) soup_link = str(link) print (soup_link) file.write(soup_link) file.flush() file.close() 

In my case, I used BeautifulSoup to write .txt with Python 3.x. He had the same problem. Just as @tsduteba said, change 'wb' in the first line to 'w'.

+1
Jul 29 '17 at 14:36
source share

just change wb to w

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

before

 outfile=open('./immates.csv','w') 
0
Sep 18 '19 at 6:02
source share



All Articles