1

I'm trying to write a short script (for a tutorial) to show which of the first 128 ASCII characters are printable, recognizable characters.

The following script using `is in string.printable' partially works, but is still triggering off of things like line feeds, etc.

Is there one or two more lines I can add to trap and replace these control characters with spaces so that the table looks nice and shows printable characters?

Written out longhand for noob-readability:

import string
lines = []
for i in range(8):
    line = []
    for j in range(16):
        k = i + j *16
        thing = chr(k)
        if thing not in string.printable:
            thing = ' '
        line.append(thing)
    lines.append(line)

for line in lines:
    print "".join(line)

IDLE:

enter image description here

Copy/paste into SE including four spaces:

   0@P`p
  !1AQaq
  "2BRbr
  #3CScs
  $4DTdt
  %5EUeu
  &6FVfv
  '7GWgw
  (8HXhx
         )9IYiy

 *:JZjz
 +;K[k{
 ,<L\l|

-=M]m} .>N^n~ /?O_o

Rich Churcher
  • 7,361
  • 3
  • 37
  • 60
uhoh
  • 3,713
  • 6
  • 42
  • 95
  • 1
    If your output and processing can handle [Unicode](http://www.unicode.org/charts/nameslist/), the [Control Pictures](http://www.unicode.org/charts/nameslist/c_2400.html) block is useful as substitution characters. – Tom Blodget Nov 17 '17 at 11:50
  • @TomBlodget After struggling with a [particularly mysterious possessive apostrophe](https://apple.stackexchange.com/q/204296/143729) I've developed a mild case of *unicodophobia*. None-the-less, the control pictures block looks very interesting and I will keep it in mind. Thanks for the info! – uhoh Nov 17 '17 at 12:32

2 Answers2

1

Updated code to replace unprintable characters as per OP's comment.

from string import printable, whitespace

replace_ch = ' '  # The character to replace non-printable characters with
whitespace_ch = ' '  # The character to replace whitespace characters with

# For console output
out_width = 8
out_height = 16

chars = [chr(i) for i in range(128)]
# Replace whitespace characters
chars = [whitespace_ch if c in whitespace else c for c in chars]
# Replace non-printable characters
chars = [c if c in printable else replace_ch for c in chars]
# Form lines for printing
lines = [[chars[i + j * out_height] for j in range(out_width)] for i in range(out_height)]

for line in lines:
    print(''.join(line))

Outputs:

   0@P`p
  !1AQaq
  "2BRbr
  #3CScs
  $4DTdt
  %5EUeu
  &6FVfv
  '7GWgw
  (8HXhx
  )9IYiy
  *:JZjz
  +;K[k{
  ,<L\l|
  -=M]m}
  .>N^n~
  /?O_o

An alternative is to simply avoid the first 32 characters of ASCII as they are defined as control characters. It may be more efficient to check the ord(c) of a character, c.

shayaan
  • 1,482
  • 1
  • 15
  • 32
  • 1
    Yep, I set the replacement character to '.' in order for them to show up, but that is a bit funky. Give me a second here to figure it out... – shayaan Nov 17 '17 at 01:01
  • 1
    I've added that in! I'm just going to set up my lines as you did then that should be done – shayaan Nov 17 '17 at 01:16
1
import string

columns = []
height = 10
whitespace_map = {
    '\t':   'TAB',
    '\n':   'LF',
    '\r':   'CR',
    '\x0b': 'VT',
    '\x0c': 'FF',
}

i = 0
j = height
while i < len(string.printable):
    column = string.printable[i:j]
    columns.append(column)
    i += height
    j += height

k = 0
while k < height:
    for column in columns:
        if k > len(column):
            break
        c = column[k]
        if c in string.whitespace and c != ' ':
            c = whitespace_map[c]
            # Alternative: c = ' '
        print('{: <5}'.format(c), end='')
    print()
    k += 1

Prints:

0    a    k    u    E    O    Y    )    =    {
1    b    l    v    F    P    Z    *    >    |
2    c    m    w    G    Q    !    +    ?    }
3    d    n    x    H    R    "    ,    @    ~
4    e    o    y    I    S    #    -    [
5    f    p    z    J    T    $    .    \    TAB
6    g    q    A    K    U    %    /    ]    LF
7    h    r    B    L    V    &    :    ^    CR
8    i    s    C    M    W    '    ;    _    VT
9    j    t    D    N    X    (    <    `    FF