15

I want to split a string in python using this code:

means="a ، b ، c"
lst=means.split("،")

but I get this error message:

SyntaxError: Non-ASCII character '\xd8' in file dict.py on line 2, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

How do I declare an encoding?

Paul Fleming
  • 24,238
  • 8
  • 76
  • 113
user1472850
  • 329
  • 2
  • 6
  • 13

2 Answers2

71

Put:

# -*- coding: UTF-8 -*-

as the first line of the file (or second line if using *nix) and save the file as UTF-8.

If you're using Python 2, use Unicode string literals (u"..."), for example:

means = u"a ، b ، c"
lst = means.split(u"،")

If you're using Python 3, string literals are Unicode already (unless marked as bytestrings b"...").

MRAB
  • 20,356
  • 6
  • 40
  • 33
  • @imrek then instead of declaring an encoding of UTF-8, declare the encoding that your file actually uses. It has nothing to do with the Python version and everything to do with the file itself. – Karl Knechtel May 24 '23 at 06:02
4

You need to declare an encoding for your file, as documented here and here.

BrenBarn
  • 242,874
  • 37
  • 412
  • 384