Python:将二进制文字文本文件转换为普通文本

问题描述 投票:-1回答:1

我有一个这种格式的文本文件:

b'Chapter 1 \xe2\x80\x93 BlaBla'
b'Boy\xe2\x80\x99s Dead.'

而且我想阅读这些行并对其进行隐蔽

Chapter 1 - BlaBla
Boy's Dead.

并将它们替换在同一文件中。我已经尝试使用print(line.encode("UTF-8", "replace"))进行编码和解码,但没有用

python encoding utf-8 web-crawler utf
1个回答
1
投票
strings = [
    b'Chapter 1 \xe2\x80\x93 BlaBla',
    b'Boy\xe2\x80\x99s Dead.',
]

for string in strings:
    print(string.decode('utf-8', 'ignore'))

--output:--
Chapter 1 – BlaBla
Boy’s Dead.

并将它们替换在同一文件中。

世界上没有计算机编程语言可以做到这一点。您必须将输出写入新文件,删除旧文件,然后将新文件重命名为旧文件。但是,python的fileinput模块可以为您执行该过程:

import fileinput as fi
import sys

with open('data.txt', 'wb') as f:
    f.write(b'Chapter 1 \xe2\x80\x93 BlaBla\n')
    f.write(b'Boy\xe2\x80\x99s Dead.\n')

with open('data.txt', 'rb') as f:
    for line in f:
        print(line)

with fi.input(
        files = 'data.txt', 
        inplace = True,
        backup = '.bak',
        mode = 'rb') as f:

    for line in f:
        string = line.decode('utf-8', 'ignore')
        print(string, end="")

~/python_programs$ python3.4 prog.py
b'Chapter 1 \xe2\x80\x93 BlaBla\n'
b'Boy\xe2\x80\x99s Dead.\n'

~/python_programs$ cat data.txt
Chapter 1 – BlaBla
Boy’s Dead.

编辑:

import fileinput as fi
import re

pattern = r"""
    \\              #Match a literal slash...
    x               #Followed by an x...
    [a-f0-9]{2}     #Followed by any hex character, 2 times 
"""

repl = ''

with open('data.txt', 'w') as f:
    print(r"b'Chapter 1 \xe2\x80\x93 BlaBla'", file=f)
    print(r"b'Boy\xe2\x80\x99s Dead.'", file=f)

with open('data.txt') as f:
    for line in f:
        print(line.rstrip()) #Output goes to terminal window

with fi.input(
        files = 'data.txt', 
        inplace = True,
        backup = '.bak') as f:

    for line in f:
        line = line.rstrip()[2:-1]
        new_line = re.sub(pattern,  "", line, flags=re.X)
        print(new_line) #Writes to file, not your terminal window

~/python_programs$ python3.4 prog.py 
b'Chapter 1 \xe2\x80\x93 BlaBla'
b'Boy\xe2\x80\x99s Dead.'

~/python_programs$ cat data.txt
Chapter 1  BlaBla
Boys Dead.

您的文件不包含二进制数据,因此您可以在text mode中读取(或写入)文件。只是正确逃避问题。

这里是第一部分:

print(r"b'Chapter 1 \xe2\x80\x93 BlaBla'", file=f)

Python将字符串中的某些backslash escape sequences转换为其他内容。 python转换的反斜杠转义序列之一的格式为:

\xNN  #=> e.g. \xe2

反斜杠转义序列长4个字符,但是python将反斜杠转义序列转换为single字符。

但是,我需要将四个字符中的每一个都写入我创建的示例文件中。为了防止python将反斜杠转义序列转换为一个字符,可以使用另一个'\'来转义开头的'\':

\\xNN

但是很懒,我不想遍历您的字符串并手动转义每个反斜杠转义序列,所以我使用了:

r"...."

r string为您转义了所有反斜杠。结果,python将\xNN序列的所有四个字符写入文件。

下一个问题是replacing a backslash in a string using a regex-我认为这是您首先要面对的问题。当文件包含\时,python会将其读入字符串作为\\以表示文字反斜杠。结果,如果文件包含四个字符:

\xe2

python将其读取为字符串:

"\\xe2"

打印时看起来像:

\xe2

底线是:如果在打印输出的字符串中可以看到'\',则字符串中的反斜杠被转义了。要查看字符串中的内容,请始终使用repr()

string = "\\xe2"
print(string)
print(repr(string))

--output:--
\xe2
'\\xe2'

请注意,如果输出中带有引号,那么您将看到字符串中的所有内容。如果输出中没有引号,则无法确定字符串中到底是什么。

要构造与字符串中的文字反斜杠匹配的正则表达式模式,简单的答案是:您需要使用两倍的反斜杠。用字符串:

"\\xe2"

您会认为模式将是:

pattern = "\\x"

但基于重复规则,您实际上需要:

pattern = "\\\\x"

还记得r字符串吗?如果对模式使用r字符串,则可以编写合理的代码,然后r字符串将转义所有斜杠,将其加倍:

pattern r"\\x"  #=> equivalent to "\\\\x"
© www.soinside.com 2019 - 2024. All rights reserved.