Python 编码不可知文件I/O错误

Python 编码不可知文件I/O错误,python,unicode,encoding,beautifulsoup,Python,Unicode,Encoding,Beautifulsoup,我试图创建的函数将读取和写入文件,而不考虑其编码(在一定程度上) 但我在尝试编写UTF-8文件时出错 UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-5136: ordinal not in range(128) 出了什么问题,我该怎么解决 def file_read_agnostic(filePath): # Read file regardless of encoding (UTF-16

我试图创建的函数将读取和写入文件,而不考虑其编码(在一定程度上)

但我在尝试编写UTF-8文件时出错

UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-5136: ordinal not in range(128)
出了什么问题,我该怎么解决

def file_read_agnostic(filePath):
    # Read file regardless of encoding (UTF-16-LE, UTF-8, ANSI)
    # Use BeautifulSoup to determine encoding

    soup = BeautifulSoup(open(filePath, 'r').read())
    encoding = soup.originalEncoding

    try:
        return soup.contents[0].decode(encoding)
    except Exception, e:
        return soup.contents[0]

def file_write_agnostic(filePath, contents):

    soup = BeautifulSoup(contents)
    encoding = soup.originalEncoding

    f = open(filePath, mode='w')
    f.write(soup.contents[0])
    f.close()

# error occurs here
file_write_agnostic("a.txt", myUTF7Content)

您需要在写入之前将文件编码为字节(发生错误是因为Python试图在写入之前将数据强制为字节,并且无法将unicode字符放入ASCII(这是默认编码)

因此需要将
f.write(soup.contents[0])
更改为
f.write(soup.contents[0].encode(encoding))
。这也将在写入的文件中保持编码