Python 打印完整的 ascii 艺术

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23623288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:14:54  来源:igfitidea点击:

Print full ascii art

pythonpython-3.xprintingutf-8ascii

提问by user3476998

I am trying to print ascii art like this:

我正在尝试像这样打印 ascii 艺术:

print(("""\

                                       ._ o o
                                       \_`-)|_
                                    ,""       \ 
                                  ,"  ## |   ? ?. 
                                ," ##   ,-\__    `.
                              ,"       /     `--._;)
                            ,"     ## /
                          ,"   ##    /


                    """).encode('utf-8'))

And the output does not look right at all.

并且输出看起来根本不正确。

What is the proper method of printing ascii art?

打印 ascii 艺术的正确方法是什么?

采纳答案by user2357112 supports Monica

encodetakes a string and encodes it into bytes. That's not what you want here; you want to just print the string directly:

encode接受一个字符串并将其编码为字节。这不是你想要的;您只想直接打印字符串:

print("""\

                                       ._ o o
                                       \_`-)|_
                                    ,""       \ 
                                  ,"  ## |   ? ?. 
                                ," ##   ,-\__    `.
                              ,"       /     `--._;)
                            ,"     ## /
                          ,"   ##    /


                    """)

If this doesn't work, your terminal is most likely not configured to display Unicode. Unfortunately, I am not particularly knowledgeable about terminal configuration; Why doesn't my terminal output unicode characters properly?may be relevant, but my ability to help is mostly limited to the Python side of things.

如果这不起作用,则您的终端很可能未配置为显示 Unicode。不幸的是,我对终端配置不是特别了解;为什么我的终端不能正确输出 unicode 字符?可能是相关的,但我提供帮助的能力主要限于 Python 方面。

回答by jfs

I get "...codec can't encode character '\u0ca0' in position..."

我得到“......编解码器无法在位置编码字符'\u0ca0'......”

If print(giraffe)fails due to an incorrect character encoding then try to set PYTHONIOENCODINGenvironment variable correctly e.g., in bash:

如果print(giraffe)由于字符编码不正确而失败,请尝试PYTHONIOENCODING正确设置环境变量,例如在 bash 中:

$ PYTHONIOENCODING=utf-8 python3 -c 'from text_art import giraffe as s; print(s)'

Do not use print(giraffe.encode('utf-8')):

不要使用print(giraffe.encode('utf-8'))

  • print()function expects a text, not bytes (unrelated: to print bytes, you could use sys.stdout.buffer.write(some_bytes))
  • how bytes are interpreted as a text is the property of your terminal, you shouldn't hardcode its settings in your code. PYTHONIOENCODINGallows you to change the encoding if necessary
  • print()函数需要一个文本,而不是字节(无关:打印字节,你可以使用sys.stdout.buffer.write(some_bytes)
  • 如何将字节解释为文本是终端的属性,您不应该在代码中对其设置进行硬编码。PYTHONIOENCODING允许您在必要时更改编码

回答by Samantha Penningtoon

print(r"""\

                                   ._ o o
                                   \_`-)|_
                                ,""       \ 
                              ,"  ## |   ? ?. 
                            ," ##   ,-\__    `.
                          ,"       /     `--._;)
                        ,"     ## /
                      ,"   ##    /


                """)

The r allows you to print raw text better especially when there is a lot of inverted commas in the picture that you are trying to print.

r 允许您更好地打印原始文本,尤其是当您尝试打印的图片中有很多引号时。

回答by Nathan Smiechowski

No need to add ''.encode('utf-8')

无需添加 ''.encode('utf-8')

我一直在 Ubuntu 机器上使用 Python 3.6.7 打印 ASCCII 艺术
__header__ = '''Content-Type: application/xml

3[92m        .---------------------------------.
3[92m        |  .---------------------------.  |
3[92m        |[]|3[94m         __   __   *       3[92m|[]|
3[92m        |  |3[94m        /  | /  | /        3[92m|  |
3[92m        |  |3[94m       (___|(___|(         3[92m|  |
3[92m        |  |3[94m       |   )|    |         3[92m|  |
3[92m        |  |3[94m       |  / |    |         3[92m|  |
3[92m        |  |3[94m /              |         |3[92m|  |
3[92m        |  |3[94m(  ___  ___  ___| ___  ___|3[92m|  |
3[92m        |  |3[94m| |   )|   )|   )|___)|   )3[92m|  |
3[92m        |  |3[94m| |__/ |__/||__/ |__  |__/ 3[92m|  |
..And more

THEN

然后

print(__header__)

And the result

结果

Ubuntu-Python3-print(ASCII)

Ubuntu-Python3-print(ASCII)

回答by Yash Shukla

print(r"""\

                               ._ o o
                               \_`-)|_
                            ,""       \ 
                          ,"  ## |   ? ?. 
                        ," ##   ,-\__    `.
                      ,"       /     `--._;)
                    ,"     ## /
                  ,"   ##    /


            """)
print(r"""\

                               ._ o o
                               \_`-)|_
                            ,""       \ 
                          ,"  ## |   ? ?. 
                        ," ##   ,-\__    `.
                      ,"       /     `--._;)
                    ,"     ## /
                  ,"   ##    /


            """)