If the source file is UTF-8 encoded, open the file like:
f = open(filepath, encoding=“utf-8”)
This will prevent error messages like “can’t decode byte at …”
If the file uses BOM, use “utf-8-sig” instead!
If the script itself contains special chars
Load the py file into text editor, that should work
For other files, you usually need to add a signature like:
-- coding: utf-8 --
If a special char is in a commen, it shouldn’t matter at all.
If it is printed to console for instance however, it will give you “can’t decode character” error.
Add .encode() to it for a ascii-literals-only byte object.
You can also decode it again to string if necessary, and ignore errors:
print(“Some special chars:”, “你好”.encode().decode(“ascii”, errors=‘ignore’))
many times i save some code snippet into a ODT file in libre office
find it easier to document it and add color ect,
so if i copy some text back to a py file it may contain some UNT-8 charaters some how
and it refuse to run and gives errors !
another is that if in some comments line with # or using “”"
then if i add an internet link it might also contains some UNT-8 characters again and
will give error!
in text editor it should give the line on which it is happening or not!
so you say that if i set at beginning of file
-- coding: utf-8 --
that it should be able to accept some UNT-8 characters!