0

I declare some variables in Bangla without any syntax error.

But when I want to print it, its gives me the error.

SyntaxError: Non-UTF-8 code starting with '\xff' in file D:/Project/Python Tutorials Repo/condition/condition.py on line 1, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

This is my script it Github: https://github.com/banglaosc/condition/blob/master/condition.py

Cant Print Bangla

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Sushen Biswas
  • 522
  • 1
  • 6
  • 24

3 Answers3

2

In your code editor, you can see that your Unicode change with another format (UTF-16LE). Try to convert from UTF-16LE to UTF-8 from the bottom right corner. It's working for me.

Unicode UTF-8 change picture

wjandrea
  • 28,235
  • 9
  • 60
  • 81
1

The encoding is at the bottom right in that PyCharm screenshot: UTF-16LE. The problem is that Python 2 will assume a file is ASCII (per PEP 263), and Python 3 will assume UTF-8 (per the tutorial).

Try switching the file encoding by clicking the UTF-16LE button at the bottom right.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • I write the code you provide at the top of my code and still the same error. – Sushen Biswas Jun 20 '20 at 05:32
  • @SushenBiswas Hmm, can you switch it to utf-8 instead? – wjandrea Jun 20 '20 at 05:33
  • @SushenBiswas maybe try removing the BOM? I'm not sure how to do that though, sorry – wjandrea Jun 20 '20 at 15:06
  • 1
    This is wrong. UTF-16 encoding **cannot be used** in Python. The problem is the bootstrapping procedure: in principle Python would have no problem with switching to UTF-16 to read the rest of the file, but it *cannot read the encoding declaration itself* - as it *defaults* to UTF-8, and UTF-16 is not ASCII-transparent - and thus cannot know to switch. – Karl Knechtel Mar 30 '23 at 11:39
  • The strange thing to me is that PyCharm offers the option to edit UTF-16 files in the first place, given that it's an IDE specifically for a language that doesn't support them. – Karl Knechtel Mar 31 '23 at 02:42
0

EDIT: The following applies to python3.5+ this will not work in python2.7

When I run your code I do not get that error. Pycharm show it as an error, but the python interpreter has no issue with the characters. The exception actually raised when running this is TypeError because the variable জুকারবার্গ is an int, and you are trying to use the + on it with a string. The following will execute with no errors.

বিল_গেটস = 20
জুকারবার্গ = 30
ওয়ারেন_বাফেট = 35
ইলন_মাস্ক = 10


if বিল_গেটস > জুকারবার্গ:
    print(str(বিল_গেটস) + "বেশি ধনি")
else:
    print(str(জুকারবার্গ) + "বেশি ধনি")
Alex
  • 1,172
  • 11
  • 31