在列表中添加非ASCII字符并将其打印到列表中 [英] add and print non-ASCII characters to a list-python
问题描述
借助我所学的python,我一直在尝试-当您键入一个短语时-将其转换为符号.我尝试了maketrans()函数,但是没有用.这是我的代码:
With the python I have learn, I have been trying to – when you type in a phrase – translate it into symbols. I tried the maketrans() function, but that didn't worked. Here's my code:
# -*- coding: utf-8 -*-
message = "ab"
output = []
broke = list(message)
limit = len(broke)
for i in range(limit):
if broke[i] == "a":
output.append("¬")
if broke[i] == "b":
output.append("∆")
如果执行print output
,我得到:['\xc2\xac']
而不是¬.有什么办法可以解决这个问题吗?
If I execute print output
I get: ['\xc2\xac']
instead of ¬. Is there any way I could get around this?
推荐答案
在python 3中,打印列表不使用字符串转义,而在python 2中则使用字符串转义,因此在python 3中执行相同的操作将获得保存结果通缉.要在Python 2中获得相同的结果,您将必须执行以下操作:
In python 3, printing lists doesn't use string escape while in Python 2 it does, so doing the same thing in python 3 will achieve the save result you wanted. To achieve the same result in Python 2, you will have to do:
# -*- coding: utf-8 -*-
import sys
message = "ab"
output = []
broke = list(message)
limit = len(broke)
for i in range(limit):
if broke[i] == "a":
output.append("¬")
if broke[i] == "b":
output.append("∆")
msg = repr(output).decode('string-escape')
print msg
输出将是:
['¬', '∆']
这篇关于在列表中添加非ASCII字符并将其打印到列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!