您正在使用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)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)