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

Shi*_*uku 141 csv html-table beautifulsoup python-3.x

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

在执行下面的python代码时将错误的表格数据保存在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)
Run Code Online (Sandbox Code Playgroud)

在最后一行上方.

dst*_*eba 272

您使用的是Python 2方法而不是Python 3.

更改:

outfile=open('./immates.csv','wb')
Run Code Online (Sandbox Code Playgroud)

至:

outfile=open('./immates.csv','w')
Run Code Online (Sandbox Code Playgroud)

并且您将获得具有以下输出的文件:

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
.....
Run Code Online (Sandbox Code Playgroud)

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

编辑添加

这是我运行的代码:

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)
Run Code Online (Sandbox Code Playgroud)

  • 为了与`csv`模块一起使用,Python 3`open`也应该有`newline =''`作为参数[[ref](https://docs.python.org/3.3/library/csv.html?突出= CSV#csv.reader)] (9认同)
  • 将 'wb' 字符串更改为 'w' 对我有用。非常感谢 (2认同)

vin*_*yll 16

我对Python3也有同样的问题.我的代码正在写入io.BytesIO().

用已io.StringIO()解决的替换.


Soh*_*Das 8

您正在以二进制模式打开 csv 文件,它应该是'w'

import csv

# open csv file in write mode with utf-8 encoding
with open('output.csv','w',encoding='utf-8',newline='')as w:
    fieldnames = ["SNo", "States", "Dist", "Population"]
    writer = csv.DictWriter(w, fieldnames=fieldnames)
    # write list of dicts
    writer.writerows(list_of_dicts) #writerow(dict) if write one row at time
Run Code Online (Sandbox Code Playgroud)


Sar*_* Ak 5

只需将 wb 更改为 w

outfile=open('./immates.csv','wb')
Run Code Online (Sandbox Code Playgroud)

outfile=open('./immates.csv','w')
Run Code Online (Sandbox Code Playgroud)