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

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

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

问题

为什么这不起作用?我在数据文件中删除了多余的反斜杠,但控制台中不显示Unicode字符?

import re 
file = open("arrayOfCards",'r')
while True:
    next_line = file.readline()
    if not next_line:
        break;
    d = next_line.split(',')
    print (re.sub(r'\\','',d[2]))
file.close()

card = ['A','diamonds','🃁','14']
print (type(card[2]), card[2])

file arrayOfCards:

A,diamonds,🃁,14
A,clubs,\\U0001F0D1,14

我得到以下输出:

[qw@qw project]$
🃁
\U0001F0D1
<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?

import re 
file = open(&quot;arrayOfCards&quot;,&#39;r&#39;)
while True:
    next_line = file.readline()
    if not next_line:
        break;
    d = next_line.split(&#39;,&#39;)
    print (re.sub(r&#39;\&#39;&#39;,&#39;&#39;,d[2]))
file.close()

card = [&#39;A&#39;,&#39;diamonds&#39;,&#39;\U0001F0C1&#39;,&#39;14&#39;]
print (type(card[2]), card[2])

file arrayOfCards:

A,diamonds,\U0001F0C1,14
A,clubs,\\U0001F0D1,14

I have next OUT:

[qw@qw project]$
\U0001F0C1
\\U0001F0D1
&lt;class &#39;str&#39;&gt; &#127169;

Please help, I can't solve the problem.

答案1

得分: 0

有关Unicode和使用`\`转义字符似乎存在一些混淆

```python
from io import StringIO
import re

f = StringIO(r&quot;&quot;&quot;A,diamonds,\U0001F0C1,14
A,clubs,\\U0001F0D1,14&quot;&quot;&quot;)

for l in f.readlines():
    d = l.split(&#39;,&#39;)
    c = re.sub(r&#39;\\\\&#39;, r&#39;\\&#39;, d[2])      # 替换双斜杠为单斜杠(类似第2行)
    print(c.encode().decode(&#39;unicode-escape&#39;)) # 解释您的Unicode字符串,同时考虑转义的\U

card = [&#39;A&#39;, &#39;diamonds&#39;, &#39;\U0001F0C1&#39;, &#39;14&#39;]
print(type(card[2]), card[2])

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


<details>
<summary>英文:</summary>

There seems to be some confusion around unicode and escaping characters with `\`:
```python
from io import StringIO
import re

f = StringIO(r&quot;&quot;&quot;A,diamonds,\U0001F0C1,14
A,clubs,\\U0001F0D1,14&quot;&quot;&quot;)

for l in f.readlines():
    d = l.split(&#39;,&#39;)
    c = re.sub(r&#39;\\\\&#39;,r&#39;\\&#39;,d[2])      # replacing double with single slash (like in 2nd line)
    print(c.encode().decode(&#39;unicode-escape&#39;)) # interpreting your unicode string while taking escaped \U into consideration

card = [&#39;A&#39;,&#39;diamonds&#39;,&#39;\U0001F0C1&#39;,&#39;14&#39;]
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;)。您必须在字符串的开头和结尾添加引号,以使其成为一个用于评估的字符串文字。最终的代码将如下所示:

import re 
file = open("arrayOfCards",'r')
while True:
    next_line = file.readline()
    if not next_line:
        break;
    d = next_line.split(',')
    d[2] = eval("''" + d[2] + "''")
    print (re.sub(r"''",'',d[2]))
file.close()

card = ['A','diamonds','🃁','14']
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:

import re 
file = open(&quot;arrayOfCards&quot;,&#39;r&#39;)
while True:
    next_line = file.readline()
    if not next_line:
        break;
    d = next_line.split(&#39;,&#39;)
    d[2] = eval(&quot;&#39;&quot; + d[2] + &quot;&#39;&quot;)
    print (re.sub(r&#39;\&#39;&#39;,&#39;&#39;,d[2]))
file.close()

card = [&#39;A&#39;,&#39;diamonds&#39;,&#39;\U0001F0C1&#39;,&#39;14&#39;]
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:

确定