user1074988
user1074988

Reputation: 27

python SyntaxError: encoding problem: windows-1255 with BOM

That's my simple code:

#!/usr/bin/env python
# -*- coding: windows-1255 -*-

str = "\u05dc\u05d9\u05d0\u05d5\u05e8"

print (str)

Could you tell me why I get this error?

python SyntaxError: encoding problem: windows-1255 with BOM

Thanks a lot

Upvotes: 0

Views: 1057

Answers (1)

Jongware
Jongware

Reputation: 22478

You saved your simple code in an encoding that explicitly includes the BOM – for a 21st century editor, that'd most likely be UTF-8. There are no non-7-bit ASCII characters in your program, so the BOM is not necessary – but computers are famously dumb and do what you ask. But at the top of your script you declare you are using an entirely type of encoding – one that does not use a BOM – and so Python complains.

The fact that you have Unicode strings in your script, by the way, does not matter. It also happens when you save a simple

#!/usr/bin/env python
# -*- coding: windows-1255 -*-

x

and try to run this.

It's sort of unexpected because to parse the encoding line at the top, Python already had to read and skip the BOM. But that encoding says that there should not have been a BOM, so there must – as Python correctly concludes – be an error somewhere.

Don't lie in the encoding line or save without BOM, and the problem disappears. If you cannot find out why your editor saves this way or are unable to find out how to properly save in the correct encoding, change the line at the top to

# -*- coding: utf-8 -*-

A very good discussion about the problems with different encodings and Unicode is The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!).

Upvotes: 0

Related Questions