简体   繁体   中英

Special characters in Python strings when writing code

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 :)

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 ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM