0

I'm trying to open a file, read the file and print out. Well this should be easy and look like this:

file = open("Testfile.txt", "r")
string = file.read()
print(string)
file.close()

So now, I want to open a file including Unicode Charcters, going above the 255 characters which can be encoded with 'charmap', as I understood right.

So what happens now is, that the Error "UnicodeEncodeError" raises and my program crashes.

My question: What do I have to do to print and read the UniCode characters correctly without raising an error?

Thx for help

Note: I do not want to get things like \U000800 or \x80, because I can't do anything with them. I need the characters, as you can pick them out of the Unicode Table.

Edit: Using Windows 7, and even if I try to output it into a file, the error occurs :/

freedome97
  • 167
  • 3
  • 15
  • When you call `print` Python needs to encode the string for your current terminal, which is where the error comes from. If you only need to output to a file, the error will not occur. However, if you need terminal output, the answer is highly dependent on what terminal/OS you are using, so you will need to mention that. – Marc J Apr 12 '16 at 21:05
  • There are only about a thousand `UnicodeEncodeError` questions on this site - try searching through some of them first. – MattDMo Apr 12 '16 at 21:28
  • @MattDMo I know that there are many questions about this error, but I saw nearly everyone I could find and no one helps me, so thats the reason i ask :) – freedome97 Apr 12 '16 at 21:39

1 Answers1

0

If you're using the command line (cmd.exe), you are unable to print unicode characters.

Try using IDLE that comes with python in order to run the code. It allows printing unicode along with typing and other things.

Bharel
  • 23,672
  • 5
  • 40
  • 80