英文:
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("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','\U0001F0C1','14']
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
<class 'str'> 🃁
Please help, I can't solve the problem.
答案1
得分: 0
有关Unicode和使用`\`转义字符似乎存在一些混淆:
```python
from io import StringIO
import re
f = StringIO(r"""A,diamonds,\U0001F0C1,14
A,clubs,\\U0001F0D1,14""")
for l in f.readlines():
d = l.split(',')
c = re.sub(r'\\\\', r'\\', d[2]) # 替换双斜杠为单斜杠(类似第2行)
print(c.encode().decode('unicode-escape')) # 解释您的Unicode字符串,同时考虑转义的\U
card = ['A', 'diamonds', '\U0001F0C1', '14']
print(type(card[2]), card[2])
注意:如果您不需要进一步的替换,可以保留import re
并将re.sub(...)
替换为c = d[2].replace('\\\\', '\\')
<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"""A,diamonds,\U0001F0C1,14
A,clubs,\\U0001F0D1,14""")
for l in f.readlines():
d = l.split(',')
c = re.sub(r'\\\\',r'\\',d[2]) # replacing double with single slash (like in 2nd line)
print(c.encode().decode('unicode-escape')) # interpreting your unicode string while taking escaped \U into consideration
card = ['A','diamonds','\U0001F0C1','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('\\\\','\\')
答案2
得分: -1
因为文件是在不执行Unicode转义的情况下读取的。您可以使用eval
手动执行它们,如下所示:d[2] = eval(""'" + d[2] + "'")
。您必须在字符串的开头和结尾添加引号,以使其成为一个用于评估的字符串文字。最终的代码将如下所示:
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("'" + d[2] + "'")
. 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("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','\U0001F0C1','14']
print (type(card[2]), card[2])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论