Answer the question
In order to leave comments, you need to log in
Strange behavior of pycharm?
Good afternoon friends!
Please help me understand what I'm doing wrong:
Create a python3 virtual environment:
cd python/envs/
virtualenv test_venv
. test_venv/bin/activate
pip install feedparser
import feedparser
rss=feedparser.parse('https://www.lostfilm.tv/rssdd.xml')
print(rss)
File "/home/nikitos/PycharmProjects/untitled2/lf.py", line 3, in <module>
print(feedparser.parse('https://www.lostfilm.tv/rssdd.xml'))
UnicodeEncodeError: 'ascii' codec can't encode characters in position 488-492: ordinal not in range(128)
Answer the question
In order to leave comments, you need to log in
Now I will clarify the situation =).
The text is either in some encoding (cp1251, utf-8, win866, ascii, etc.), then these are bytes; or in unicode (this is, as it were, an abstract representation of characters), then these are strings of abstract unicode characters.
All files, I/O streams, etc. in our computer it works with bytes, not with abstract Unicode characters. This means that Unicode must be encoded into a specific encoding before being output to a file or console.
So, for example, the ascii encoding only supports standard 128 characters, and if you try to convert the letter "Ж" into it (explicitly or implicitly), you will get the same error as yours. It must be assumed that the parse method in your case returns unicode, and the print statement does an implicit conversion to the default encoding (ascii, judging by the error message).
It remains to find out in which cases how the default encoding is determined.
Artem Klimenko correctly suggested in his answer to check what is taken as the default encoding in both cases.
However, the solution to the problem should be to explicitly convert the text to the desired encoding. In such cases, I adhere to the following rules:
I emphasize. If the output stream is configured for ascii, and non-ascii characters may come across in our program, then you need to convert the text to some kind of encoding (see above)), otherwise we don’t touch anything and write Unicode.
Thank you very much!
In pycharm, I changed the encoding for the project to utf8 and everything worked.
Sergey Pankov , special thanks for the educational program, after reading, a mosaic of understanding began to take shape in my head :)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question