在'b \'stringdata'python 3中删除b

时间:2019-12-22 20:02:42

标签: python string byte decode encode

在python 2中,程序输出以字节为单位重定向到文件

-文件内容---

b'data1'
b'data2'

以字符串形式读取文件时,我得到'b\'data1'\r\n'...

当读为二进制文件时,我得到b'b\'data1\r\n'...

我希望列表中的所有行都为'data1'...'data2',而没有前导b

我尝试解码'utf-8',但是该行是带有'b\''的字符串,无法转换

我在pyhon2中读起来像

if not sys.stdin.isatty():
    input_file = BytesIO(sys.stdin.read())

但是在python3中,我有时尝试在打印之前进行解码,并使用管道进行重定向,但有时会出现此错误

File "Python38-32\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u2011' in position 373877: character maps to <undefined>

谢谢。

0 个答案:

没有答案