Skip to main content
 首页 » 编程设计

python之打印单引号在 .txt 文件中变成奇怪的字符

2024年12月31日7落叶无声

我用 Selenium 抓取了一些句子,然后将结果打印到一个 .txt 文件,但它不能显示 ' 而是一些奇怪的字符:

原句:

I don't think so. 

在 .txt 文件中:

I don? think so. 

我已经指定了.txt编码为“utf-8”,我该怎么办?

请您参考如下方法:

为此,您需要打开文件以使用“utf-8”写入:

with open("file.txt", "w", encoding="utf-8") as file 
 
    file.write("your_text") 

希望对你有帮助!