如何使用python将从web复制到txt文件的文本 [英] how to read text copied from web to txt file using python
问题描述
f = open(sample.txt)
print .read())
如果我自己输入了txt文件,它工作正常。但是当我从网络上的新闻文章中复制文本时,会产生以下错误:
UnicodeEncodeError:'charmap'在位置738处编码字符'\\\—':字符映射到未定义
我尝试更改我读到某个地方的Notepad ++编码设置为UTF-8,这是因为
我还尝试使用:
f = open(sample.txt,encoding ='utf-8')
$ b $来自这里的b 但它仍然没有起作用。 p>你在Windows上,试图打印到控制台。 print()正在抛出异常。 Windows控制台本身只支持8位代码页,所以你所在地区以外的任何内容都会中断(尽管有人对chcp 65001有所说)。 您需要安装并使用 https://github.com/Drekin/win-unicode-console 。该模块在控制台API的低级别上进行交谈,支持输入和输出的多字节字符。 或者,不要打印到控制台并将输出写入文件,并使用编码打开。例如: 确保使用正确的编码打开文件。 I'm learning how to read text files. I used this way: It worked fine if I typed the txt file myself. But when I copied text from a news article on the web, it produced the following error: I tried changing the Encoding setting in Notepad++ to UTF-8 as I read somewhere it is due to that I also tried using: from here But it still didn't work. You're on Windows and trying to print to the console. The print() is throwing the exception. The Windows console only natively supports 8bit code pages, so anything outside of your region will break (despite what people say about chcp 65001). You need to install and use https://github.com/Drekin/win-unicode-console. This module talks at a low-level to the console API, giving support for multi-byte characters, for input and output. Alternatively, don't print to the console and write your output to a file, opened with an encoding. For example: Ensure you open the file with the correct encoding. 这篇关于如何使用python将从web复制到txt文件的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
with open(myoutput.log,w,encoding =utf-8)as my_log :
my_log.write(body)
f=open("sample.txt")
print(f.read())
UnicodeEncodeError: 'charmap' codec can't encode charater '\u2014' in position 738: character maps to undefined
f=open("sample.txt",encoding='utf-8')
with open("myoutput.log", "w", encoding="utf-8") as my_log:
my_log.write(body)