1

I have following line in my code:

if "♠" in text:
    do_something()

When working no some UTF-16 encoded text files. It works but it looks kind of silly (to me). Is there any way to write something along the lines of "\code for this character here" instead so it works on text data opened with UTF-16 encoding ? Also how do I go around using this in regex ? Say I want to match every line beginning with ♠ or ♥ symbol.

Thanks for help :)

1 Answer 1

5

If you want a symbolic name, you can use \N{unicode character name}:

'\N{BLACK SPADE SUIT}'

(Name as found on http://www.fileformat.info/info/unicode/char/2660/index.htm).

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

3 Comments

It's probably something silly but I am new to this and this line gives syntax error
My bad, you're using Python 3.2. I removed the u (that was specific to Python 2.x). Note that the u'...' syntax is valid again in Python 3.3.
Great, now '\N{BLACK SPADE SUIT}' in "♠ J 10 9 5" returns True; that's what I need

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.