Python从文件中读取文本未找到撇号

发布于 2025-01-10 16:51:08 字数 537 浏览 0 评论 0原文

用于清理文本的函数

def clean_before_tok(text):
    text=text.replace("'"," ")
    exclude=[" le "," la "," l "," un "," une "," du "," de "," les "," des "," s "," d "]
    for e in exclude:
        text=text.replace(e," ")
    return text

我可以在宠物示例上测试它

test=clean_before_tok("dlkj dfg le se d'ac")
print(test)
>>> dlkj dfg se ac

但是当从文件中读取时

generated_text=open("text-like.txt", 'rb').read().decode(encoding='utf-8')

它没有找到替换撇号。是否存在编码缺陷?

A function for cleaning a text

def clean_before_tok(text):
    text=text.replace("'"," ")
    exclude=[" le "," la "," l "," un "," une "," du "," de "," les "," des "," s "," d "]
    for e in exclude:
        text=text.replace(e," ")
    return text

I can test this on a pet example

test=clean_before_tok("dlkj dfg le se d'ac")
print(test)
>>> dlkj dfg se ac

But when reading from file with

generated_text=open("text-like.txt", 'rb').read().decode(encoding='utf-8')

It's not finding-replacing apostrophes. Is there an encoding flaws?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

城歌 2025-01-17 16:51:08

为了检查文件的编码,您可以将其打印为字节

>>> with open("my-file.txt", "rb") as file:
...     b_file = file.read()
>>> print(b_file)

如果撇号显示为撇号,那就很奇怪了。通常,该问题可以通过文本中出现奇怪的 \xABAB 可以是任何大写或小写字母,它们代表非 ASCII 字节)来解释。

In order to check the encoding of the file, you may print it as bytes

>>> with open("my-file.txt", "rb") as file:
...     b_file = file.read()
>>> print(b_file)

If the apostrophes shows like apostrophes it's very weird. Normally the issue will be explained by the presence of weird \xAB (AB can be any letters uppercase or lowercase, they represent a non-ASCII byte) in your text.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文