0

I am trying to split a string by '£'. I have tried the following:

example = "£2.99£14.99"
example.split("£")

and:

example = "£2.99£14.99"
example.split("£".encode("utf-8"))

and:

example = "£2.99£14.99"
example.split("£".encode("utf-8", "ignore"))

They all give the following error:

SyntaxError: Non-ASCII character '\xc2' in file example.py on line 38, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

I am not sure how to proceed. Can someone assist?

Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
Jay Webster
  • 179
  • 1
  • 3
  • 12
  • Have you tried reading the link in the error message? What did you find there, and did you have trouble applying the advice? – Martijn Pieters Aug 05 '18 at 11:36
  • @Thefourthbird: then at least use an example site that uses Python 2, not 3, and *use the correct syntax*, which includes making it the first or second line (not the third). – Martijn Pieters Aug 05 '18 at 11:39
  • Instead of declaring the encoding, you could also use `\xc2\xa3` to encode the pound sign as UTF-8 in the string literal. – Martijn Pieters Aug 05 '18 at 11:39
  • I think you should use 'p' instead of the Pound Symbol. 'p' is included in ASCII. – Timmy Aug 05 '18 at 11:46

1 Answers1

0

As explained in the link from your error:

Python will default to ASCII as standard encoding if no other encoding hints are given.

To define a source code encoding, a magic comment must be placed into the source files either as first or second line in the file.


So you need to define your encoding at the beginning of your example.py file.

Exemple, if you want UTF-8 encoding, use the following line:

# -*- coding: utf-8 -*-
Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56