How to allow special characters in Python?

Asked

Viewed 5,942 times

3

In my program I want to include characters like these in the print: , and a few more of these. I’m trying to take an array with numbers and turn them into these characters, so it looks like a map/maze.

Note: I want a position [x][y] of the matrix to receive this character as content.

for i in range(10):
    for j in range(10):
        if matriz[i][j] == 0:
            sqm[i][j] = " "
        elif matriz[i][j] == 1:
            sqm[i][j] = "0"
        elif matriz[i][j] == 2:
            sqm[i][j] = u("U+25AC")
for i in range(10):
    for j in range(10):
        print sqm[i][j],
    print ""

This way it needs to be already included inside the matrix.

At the beginning of the code I already use:

# -*- coding: utf-8 -*-

But it only covers accentuation. How do I fix it?

2 answers

1

To do this, just look for the Unicode characters that you left "printar" and use the command:

print(u'\u2551')
║

Example:

box = [u'\u2550',u'\u2551',u'\u2552',u'\u2553',u'\u2554',u'\u2555',u'\u2556',
u'\u2557',u'\u2558',u'\u2559',u'\u225A']

print(box)
['═', '║', '╒', '╓', '╔', '╕', '╖', '╗', '╘', '╙', '≚']

You can see the full table of characters called "box Drawing" at that link., or if you need (and have patience) you can check all Unicode tables, here.

Run the code in repl.it.

Edited:
After the author of the question edited it and placed the code.

Observing the code, gives to realize the error exactly where the objective is to assign the character to the position of the matrix: sqm[i][j] = u("U+25AC"), probably this notation must have been deducted of this table, or something similar. But when the table U+25AC, the python conversion should become u'\u25ac', so just change the line:

sqm[i][j] = u("U+25AC")

For:

sqm[i][j] = u'\u25ac'

See this character added to the table in the new version, repl.it.

0

Hello, you can try to find the character code you need here https://unicode-table.com/pt/#ipa-Extensions and so do the following:

For example for the python character ' 2:

print u"\u00a9"

outworking:

©

or in python 3:

print("\u00a9")

outworking:

©

  • I don’t think I explained it properly, but I’ll fix it. I want this character to be included within the pq matrix I print character by character of the matrix on the screen. Ai would be included in some position [x][y], and not just printed on the screen

Browser other questions tagged

You are not signed in. Login or sign up in order to post.