<legend id='eStmN'><style id='eStmN'><dir id='eStmN'><q id='eStmN'></q></dir></style></legend>
<tfoot id='eStmN'></tfoot>

  1. <small id='eStmN'></small><noframes id='eStmN'>

    1. <i id='eStmN'><tr id='eStmN'><dt id='eStmN'><q id='eStmN'><span id='eStmN'><b id='eStmN'><form id='eStmN'><ins id='eStmN'></ins><ul id='eStmN'></ul><sub id='eStmN'></sub></form><legend id='eStmN'></legend><bdo id='eStmN'><pre id='eStmN'><center id='eStmN'></center></pre></bdo></b><th id='eStmN'></th></span></q></dt></tr></i><div id='eStmN'><tfoot id='eStmN'></tfoot><dl id='eStmN'><fieldset id='eStmN'></fieldset></dl></div>

      • <bdo id='eStmN'></bdo><ul id='eStmN'></ul>
    2. TypeError:需要一个类似字节的对象,而不是 python 和 CSV 中的“str"

      TypeError: a bytes-like object is required, not #39;str#39; in python and CSV(TypeError:需要一个类似字节的对象,而不是 python 和 CSV 中的“str)

    3. <legend id='vOVOc'><style id='vOVOc'><dir id='vOVOc'><q id='vOVOc'></q></dir></style></legend>

      <small id='vOVOc'></small><noframes id='vOVOc'>

      <tfoot id='vOVOc'></tfoot>

        <i id='vOVOc'><tr id='vOVOc'><dt id='vOVOc'><q id='vOVOc'><span id='vOVOc'><b id='vOVOc'><form id='vOVOc'><ins id='vOVOc'></ins><ul id='vOVOc'></ul><sub id='vOVOc'></sub></form><legend id='vOVOc'></legend><bdo id='vOVOc'><pre id='vOVOc'><center id='vOVOc'></center></pre></bdo></b><th id='vOVOc'></th></span></q></dt></tr></i><div id='vOVOc'><tfoot id='vOVOc'></tfoot><dl id='vOVOc'><fieldset id='vOVOc'></fieldset></dl></div>

              <tbody id='vOVOc'></tbody>
            • <bdo id='vOVOc'></bdo><ul id='vOVOc'></ul>
              • 本文介绍了TypeError:需要一个类似字节的对象,而不是 python 和 CSV 中的“str"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

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

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

                在执行以下 python 代码以将 HTML 表数据保存在 Csv 文件中时出现上述错误.不知道怎么搭车.请帮帮我.

                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)
                

                在最后一行的上方.

                推荐答案

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

                You are using Python 2 methodology instead of Python 3.

                变化:

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

                收件人:

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

                在 Python 3 中,csv 采用文本模式输入,而在 Python 2 中采用二进制模式.

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

                编辑添加

                这是我运行的代码:

                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"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)
                Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)
                Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)
                Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)
                Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)
                Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)
              • <small id='7FDtc'></small><noframes id='7FDtc'>

                    <bdo id='7FDtc'></bdo><ul id='7FDtc'></ul>
                    • <i id='7FDtc'><tr id='7FDtc'><dt id='7FDtc'><q id='7FDtc'><span id='7FDtc'><b id='7FDtc'><form id='7FDtc'><ins id='7FDtc'></ins><ul id='7FDtc'></ul><sub id='7FDtc'></sub></form><legend id='7FDtc'></legend><bdo id='7FDtc'><pre id='7FDtc'><center id='7FDtc'></center></pre></bdo></b><th id='7FDtc'></th></span></q></dt></tr></i><div id='7FDtc'><tfoot id='7FDtc'></tfoot><dl id='7FDtc'><fieldset id='7FDtc'></fieldset></dl></div>
                        <tfoot id='7FDtc'></tfoot><legend id='7FDtc'><style id='7FDtc'><dir id='7FDtc'><q id='7FDtc'></q></dir></style></legend>

                          <tbody id='7FDtc'></tbody>