Domanda

I am trying to print ascii art like this:

print(("""\

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


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

And the output does not look right at all.

What is the proper method of printing ascii art?

È stato utile?

Soluzione

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

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.

Altri suggerimenti

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.

print(r"""\

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


            """)
print(r"""\

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


            """)

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

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

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

Do not use 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. PYTHONIOENCODING allows you to change the encoding if necessary

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

I print ASCCII art all the time using Python 3.6.7 on Ubuntu machines
__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

THEN

print(__header__)

And the result

Ubuntu-Python3-print(ASCII)

print(r"""\

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


            """)

this will type or print this in raw text

to solve this issue I just added the header '# coding=utf-8' at the top level of the file and it works. Code

Result of the script

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top