我正在尝试打印这样的ascii艺术:
print(("""\
._ o o
\_`-)|_
,"" \
," ## | ಠ ಠ.
," ## ,-\__ `.
," / `--._;)
," ## /
," ## /
""").encode('utf-8'))
输出看起来并不正确。
印刷ascii艺术的正确方法是什么?
答案 0 :(得分:9)
encode
接受一个字符串并将其编码为字节。这不是你想要的;你想直接打印字符串:
print("""\
._ o o
\_`-)|_
,"" \
," ## | ಠ ಠ.
," ## ,-\__ `.
," / `--._;)
," ## /
," ## /
""")
如果这不起作用,您的终端很可能未配置为显示Unicode。不幸的是,我对终端配置并不是特别了解; Why doesn't my terminal output unicode characters properly?可能是相关的,但我的帮助能力主要限于Python方面。
答案 1 :(得分:5)
print(r"""\
._ o o
\_`-)|_
,"" \
," ## | ಠ ಠ.
," ## ,-\__ `.
," / `--._;)
," ## /
," ## /
""")
r允许您更好地打印原始文本,尤其是当您尝试打印的图片中有许多引号时。
答案 2 :(得分:2)
我得到" ...编解码器无法对字符' \ u0ca0'进行编码在位置......"
如果print(giraffe)
因字符编码错误而失败,请尝试正确设置PYTHONIOENCODING
环境变量,例如bash:
$ PYTHONIOENCODING=utf-8 python3 -c 'from text_art import giraffe as s; print(s)'
请勿使用print(giraffe.encode('utf-8'))
:
print()
函数需要一个文本,而不是字节(不相关:打印字节,你可以使用sys.stdout.buffer.write(some_bytes)
)PYTHONIOENCODING
允许您在必要时更改编码答案 3 :(得分:1)
使用PyPi的python ASCII艺术-链接here。 (除非回复,否则没有足够的声誉来发表评论。)
答案 4 :(得分:0)
''.encode('utf-8')
__header__ = '''Content-Type: application/xml
\033[92m .---------------------------------.
\033[92m | .---------------------------. |
\033[92m |[]|\033[94m __ __ * \033[92m|[]|
\033[92m | |\033[94m / | / | / \033[92m| |
\033[92m | |\033[94m (___|(___|( \033[92m| |
\033[92m | |\033[94m | )| | \033[92m| |
\033[92m | |\033[94m | / | | \033[92m| |
\033[92m | |\033[94m / | |\033[92m| |
\033[92m | |\033[94m( ___ ___ ___| ___ ___|\033[92m| |
\033[92m | |\033[94m| | )| )| )|___)| )\033[92m| |
\033[92m | |\033[94m| |__/ |__/||__/ |__ |__/ \033[92m| |
..And more
之后
print(__header__)
答案 5 :(得分:0)
print(r"""\
._ o o
\_`-)|_
,"" \
," ## | ಠ ಠ.
," ## ,-\__ `.
," / `--._;)
," ## /
," ## /
""")
print(r"""\
._ o o
\_`-)|_
,"" \
," ## | ಠ ಠ.
," ## ,-\__ `.
," / `--._;)
," ## /
," ## /
""")
答案 6 :(得分:0)
打印(r"""\
._ o o
\_`-)|_
,"" \
," ## | ಠ ಠ.
," ## ,-\__ `.
," / `--._;)
," ## /
," ## /
""")
这将以原始文本形式输入或打印