1

I get the following error while trying to search the string below

ERROR:

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

STRING:

دَّثَنَا عَبْدَانُ، قَالَ أَخْبَرَنَا عَبْ

CODE:

arabic_hadith = "دَّثَنَا عَبْدَانُ، قَالَ أَخْبَرَنَا عَبْ"
arabic_hadith.encode('utf8')
print arabic_hadith
if "الجمعة" in arabic_hadith:‎
    day = "5"
else:
    day = ""

1 Answer 1

4

You have a byte string, not a unicode value. Trying to encode a byte string in Python 2 means that Python will first try to decode it to unicode so that it can then encode.

Use unicode values here instead, and make sure you set the codec at the top of the file first. See PEP 263 - Defining Python Source Code Encodings (which your error message pointed you to).

Note that there is no need to encode to UTF8 here, that'll only complicate text comparisons:

# encoding: utf8
arabic_hadith = u"دَّثَنَا عَبْدَانُ، قَالَ أَخْبَرَنَا عَبْ"
print arabic_hadith
if u"الجمعة" in arabic_hadith:‎
    day = "5"
else:
    day = ""

Rule of thumb: decode bytes from incoming sources (files, network data) to Unicode, process only Unicode in your program, and only encode again for any outgoing data.

I urge you to read:

before you continue.

Sign up to request clarification or add additional context in comments.

10 Comments

I always have problems with Unicode... :( I tried this and it failed with the same error :(
what if arabic_hadith is a variable? doing this is doesn't solve it: arabic_hadith = u(arabic_hadith)
@Ossama: Nope, it would not. arabic_hadith.decode('utf8') would though.
@Ossama: You need to add a line, at the top (first or second line of the file) starting with #, containing the word coding followed by : or = plus the codec used to save the file.
@Ossama: you could start by reading the link the error message provided. It is hotlinked in my answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.