如何在Python中将扑克牌的Unicode符号显示到终端?

huangapple go评论73阅读模式
英文:

How to display Unicode symbols of playing card in Python to the terminal?

问题

  1. 为什么这不起作用?我在数据文件中删除了多余的反斜杠,但控制台中不显示Unicode字符?
  2. import re
  3. file = open("arrayOfCards",'r')
  4. while True:
  5. next_line = file.readline()
  6. if not next_line:
  7. break;
  8. d = next_line.split(',')
  9. print (re.sub(r'\\','',d[2]))
  10. file.close()
  11. card = ['A','diamonds','🃁','14']
  12. print (type(card[2]), card[2])

file arrayOfCards:

  1. A,diamonds,🃁,14
  2. A,clubs,\\U0001F0D1,14

我得到以下输出:

  1. [qw@qw project]$
  2. 🃁
  3. \U0001F0D1
  4. <class 'str'> 🃁

请帮忙,我解决不了这个问题。

英文:

Why doesn't this work? I remove an extra backslash in a data file, but the Unicode characters are not displayed in the console?

  1. import re
  2. file = open(&quot;arrayOfCards&quot;,&#39;r&#39;)
  3. while True:
  4. next_line = file.readline()
  5. if not next_line:
  6. break;
  7. d = next_line.split(&#39;,&#39;)
  8. print (re.sub(r&#39;\&#39;&#39;,&#39;&#39;,d[2]))
  9. file.close()
  10. card = [&#39;A&#39;,&#39;diamonds&#39;,&#39;\U0001F0C1&#39;,&#39;14&#39;]
  11. print (type(card[2]), card[2])

file arrayOfCards:

  1. A,diamonds,\U0001F0C1,14
  2. A,clubs,\\U0001F0D1,14

I have next OUT:

  1. [qw@qw project]$
  2. \U0001F0C1
  3. \\U0001F0D1
  4. &lt;class &#39;str&#39;&gt; &#127169;

Please help, I can't solve the problem.

答案1

得分: 0

  1. 有关Unicode和使用`\`转义字符似乎存在一些混淆
  2. ```python
  3. from io import StringIO
  4. import re
  5. f = StringIO(r&quot;&quot;&quot;A,diamonds,\U0001F0C1,14
  6. A,clubs,\\U0001F0D1,14&quot;&quot;&quot;)
  7. for l in f.readlines():
  8. d = l.split(&#39;,&#39;)
  9. c = re.sub(r&#39;\\\\&#39;, r&#39;\\&#39;, d[2]) # 替换双斜杠为单斜杠(类似第2行)
  10. print(c.encode().decode(&#39;unicode-escape&#39;)) # 解释您的Unicode字符串,同时考虑转义的\U
  11. card = [&#39;A&#39;, &#39;diamonds&#39;, &#39;\U0001F0C1&#39;, &#39;14&#39;]
  12. print(type(card[2]), card[2])

注意:如果您不需要进一步的替换,可以保留import re并将re.sub(...)替换为c = d[2].replace(&#39;\\\\&#39;, &#39;\\&#39;)

  1. <details>
  2. <summary>英文:</summary>
  3. There seems to be some confusion around unicode and escaping characters with `\`:
  4. ```python
  5. from io import StringIO
  6. import re
  7. f = StringIO(r&quot;&quot;&quot;A,diamonds,\U0001F0C1,14
  8. A,clubs,\\U0001F0D1,14&quot;&quot;&quot;)
  9. for l in f.readlines():
  10. d = l.split(&#39;,&#39;)
  11. c = re.sub(r&#39;\\\\&#39;,r&#39;\\&#39;,d[2]) # replacing double with single slash (like in 2nd line)
  12. print(c.encode().decode(&#39;unicode-escape&#39;)) # interpreting your unicode string while taking escaped \U into consideration
  13. card = [&#39;A&#39;,&#39;diamonds&#39;,&#39;\U0001F0C1&#39;,&#39;14&#39;]
  14. print(type(card[2]), card[2])

Note: if you don't need further substitutions, you can leave import re and replace re.sub(...) with c = d[2].replace(&#39;\\\\&#39;,&#39;\\&#39;)

答案2

得分: -1

因为文件是在不执行Unicode转义的情况下读取的。您可以使用eval手动执行它们,如下所示:d[2] = eval("&quot;&#39;&quot; + d[2] + &quot;&#39;&quot;)。您必须在字符串的开头和结尾添加引号,以使其成为一个用于评估的字符串文字。最终的代码将如下所示:

  1. import re
  2. file = open("arrayOfCards",'r')
  3. while True:
  4. next_line = file.readline()
  5. if not next_line:
  6. break;
  7. d = next_line.split(',')
  8. d[2] = eval("''" + d[2] + "''")
  9. print (re.sub(r"''",'',d[2]))
  10. file.close()
  11. card = ['A','diamonds','🃁','14']
  12. print (type(card[2]), card[2])
英文:

It is because the file is read without executing unicode escapes. You can execute them manually using eval like this: d[2] = eval(&quot;&#39;&quot; + d[2] + &quot;&#39;&quot;). You have to add quotes to start and end of string to make it a string literal for evaluation. Final code would look like this:

  1. import re
  2. file = open(&quot;arrayOfCards&quot;,&#39;r&#39;)
  3. while True:
  4. next_line = file.readline()
  5. if not next_line:
  6. break;
  7. d = next_line.split(&#39;,&#39;)
  8. d[2] = eval(&quot;&#39;&quot; + d[2] + &quot;&#39;&quot;)
  9. print (re.sub(r&#39;\&#39;&#39;,&#39;&#39;,d[2]))
  10. file.close()
  11. card = [&#39;A&#39;,&#39;diamonds&#39;,&#39;\U0001F0C1&#39;,&#39;14&#39;]
  12. print (type(card[2]), card[2])

huangapple
  • 本文由 发表于 2023年7月10日 21:33:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76654273.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定