简体   繁体   English

编写代码时Python字符串中的特殊字符

[英]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. 工作时,没有一些UTF-16编码的文本文件。 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 ? 有没有什么办法可以在“此字符的\\ code”行中写点东西,以便它对使用UTF-16编码打开的文本数据起作用? 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{unicode character name}

'\N{BLACK SPADE SUIT}'

(Name as found on http://www.fileformat.info/info/unicode/char/2660/index.htm ). (名称位于http://www.fileformat.info/info/unicode/char/2660/index.htm上 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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