簡體   English   中英

'utf-8' 編解碼器無法解碼 position 中的字節 0x80 3131:無效的起始字節':在讀取 xml 文件時

[英]'utf-8' codec can't decode byte 0x80 in position 3131: invalid start byte': while reading xml files

我想定義一個 function 可以在目錄中的每個 xml 文件上實現,以便解析它並從 Z6A8064B5DF479455500553C47C5507 中的標簽中獲取內容


from xml.etree import ElementTree

def func(path, filename):

    for filename in os.listdir(path):
        with open(os.path.join(path, filename)) as file:
        # Read each line in the file, readlines() returns a list of lines
            content = file.readlines()
        # Combine the lines in the list into a string
            content = "".join(content)
            bs_content = bs(content, "lxml")

            headline = bs_content.find_all("headline")
            eventtitle = bs_content.find_all("eventtitle")
            city = bs_content.find_all("city")
            companyname = bs_content.find_all("companyname")
            companyticker = bs_content.find_all("companyticker")
            startdate = bs_content.find_all("startdate")
            eventstory = bs_content.find_all("eventstory")

            data = []
            for i in range(0,len(companyname)):
                rows = [companyname[i].get_text(),headline[i].get_text(),
                       city[i].get_text(),eventtitle[i].get_text(),
                       companyticker[i].get_text(),startdate[i].get_text(),
                      eventstory[i].get_text()]
                data.append(rows)
 
    df = pd.DataFrame(data,columns = ['companyname','headline',
                                  'city','eventtitle','companyticker',
                                  'startdate','eventstory'], dtype = float)

當我調用 function 時,我收到此錯誤。 不幸的是,每個現有的解決方案都不適用於我。

func('./Calls/', '1000015_T.xml')
---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
Input In [58], in <module>
----> 1 func('./Calls/', '1000015_T.xml')

Input In [57], in func(path, filename)
      7 for filename in os.listdir(path):
      8     with open(os.path.join(path, filename)) as file:
      9     # Read each line in the file, readlines() returns a list of lines
---> 10         content = file.readlines()
     11     # Combine the lines in the list into a string
     12         content = "".join(content)

File /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/codecs.py:322, in BufferedIncrementalDecoder.decode(self, input, final)
    319 def decode(self, input, final=False):
    320     # decode input (taking the buffer into account)
    321     data = self.buffer + input
--> 322     (result, consumed) = self._buffer_decode(data, self.errors, final)
    323     # keep undecoded input until the next call
    324     self.buffer = data[consumed:]

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 3131: invalid start byte

也許您還可以幫助我進行代碼優化。 我的任務是獲取 2k xml 文件的內容,到目前為止,我決定定義一個 function 然后使用 pandarallel:parallel_apply(func)

輸入文件不是 UTF-8,可能是其他代碼頁。

確定正確的編碼是什么,並相應地更改您的程序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM