#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0

    Question about forming columns for an Ascii Chart


    Hi everyone, I'm trying to use the 'GiveAsciiChart5()' function to return a string that when printed, is an ascii chart and 4 entries across while using the 'chr' function. I only want to use the ascii values from 32 to 126, inclusive.

    Here's what I have so far...
    Any ideas how to clean this up?
    The output gives me columns, but isn't very neat or table-like.
    Code:
     
    def GiveAsciiChart5():     
        count = 4     
        for value in range(32, 127):         
            count += 1         
            if count % 4 == 0:              
                print "\n"         
            print "ASCII Value:", (value),         
            print " Character: ", chr(value),          
    
    def Main():     
        GiveAsciiChart5()  
    
    if __name__ == '__main__':    
        Main()

    Output:
    Code:
    GiveAsciiChart5() 
    ASCII Value: 32  Character:    ASCII Value: 33  Character:  ! ASCII Value: 34  Character:  "   ASCII Value: 35  Character:  # 
    ASCII Value: 36  Character:  $ ASCII Value: 37  Character:  % ASCII Value: 38  Character:  &   ASCII Value: 39  Character:  ' 
    ASCII Value: 40  Character:  ( ASCII Value: 41  Character:  ) ASCII Value: 42  Character:  *   ASCII Value: 43  Character:  + ASCII Value: 44  Character:  , ASCII Value: 45  Character:  - ASCII Value: 46  Character:  .   ASCII Value: 47  Character:  / ASCII Value: 48  Character:  0 ASCII Value: 49  Character:  1 ASCII Value: 50  Character:  2   ASCII Value: 51  Character:  3 ASCII Value: 52  Character:  4 ASCII Value: 53  Character:  5 ASCII Value: 54  Character:  6   ASCII Value: 55  Character:  7 ASCII Value: 56  Character:  8 ASCII Value: 57  Character:  9 ASCII Value: 58  Character:  :   ASCII Value: 59  Character:  ; ASCII Value: 60  Character:  < ASCII Value: 61  Character:  = ASCII Value: 62  Character:  >   ASCII Value: 63  Character:  ? ASCII Value: 64  Character:  @ ASCII Value: 65  Character:  A ASCII Value: 66  Character:  B   ASCII Value: 67  Character:  C ASCII Value: 68  Character:  D ASCII Value: 69  Character:  E ASCII Value: 70  Character:  F   ASCII Value: 71  Character:  G ASCII Value: 72  Character:  H ASCII Value: 73  Character:  I ASCII Value: 74  Character:  J   ASCII Value: 75  Character:  K ASCII Value: 76  Character:  L ASCII Value: 77  Character:  M ASCII Value: 78  Character:  N   ASCII Value: 79  Character:  O ASCII Value: 80  Character:  P ASCII Value: 81  Character:  Q ASCII Value: 82  Character:  R   ASCII Value: 83  Character:  S ASCII Value: 84  Character:  T ASCII Value: 85  Character:  U ASCII Value: 86  Character:  V   ASCII Value: 87  Character:  W ASCII Value: 88  Character:  X ASCII Value: 89  Character:  Y ASCII Value: 90  Character:  Z   ASCII Value: 91  Character:  [ ASCII Value: 92  Character:  \ ASCII Value: 93  Character:  ] ASCII Value: 94  Character:  ^   ASCII Value: 95  Character:  _ ASCII Value: 96  Character:  ` ASCII Value: 97  Character:  a ASCII Value: 98  Character:  b   ASCII Value: 99  Character:  c ASCII Value: 100  Character:  d ASCII Value: 101  Character:  e ASCII Value: 102  Character:  f   ASCII Value: 103  Character:  g ASCII Value: 104  Character:  h ASCII Value: 105  Character:  i ASCII Value: 106  Character:  j   ASCII Value: 107  Character:  k ASCII Value: 108  Character:  l ASCII Value: 109  Character:  m ASCII Value: 110  Character:  n   ASCII Value: 111  Character:  o ASCII Value: 112  Character:  p ASCII Value: 113  Character:  q ASCII Value: 114  Character:  r   ASCII Value: 115  Character:  s ASCII Value: 116  Character:  t ASCII Value: 117  Character:  u ASCII Value: 118  Character:  v   ASCII Value: 119  Character:  w ASCII Value: 120  Character:  x ASCII Value: 121  Character:  y ASCII Value: 122  Character:  z   ASCII Value: 123  Character:  { ASCII Value: 124  Character:  | ASCII Value: 125  Character:  } ASCII Value: 126  Character:  ~


    The output was too time-consuming to fix for posting on here, but I tried to give a small example of at least the first section of it. It has 4 pairs going across. Ascii values... and character values...it just doesn't always line up perfectly.
    Please let me know if you have any ideas.
    Thank you very much!
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    39N 104.28W
    Posts
    158
    Rep Power
    3

    Talking


    Originally Posted by ColinC02
    Hi everyone, I'm trying to use the 'GiveAsciiChart5()' function to return a string that when printed, is an ascii chart and 4 entries across while using the 'chr' function. I only want to use the ascii values from 32 to 126, inclusive.
    Here's what I use to print all the ascii characters to a file in 4 columns. I write it out to a file but you change the "write" to "print" easily enough. Maybe you can use some of this. The first stuff, "a=..." is just to account for the unprintable characters.
    Code:
    a=('Null character','Start of Header','Start of Text','End of Text',
    'End of Transmission','Enquiry','Acknowledgment','Bell','Backspace',
    'Horizontal Tab','Line feed','Vertical Tab','Form feed','Carriage return',
    'Shift Out','Shift In','Data Link Escape','Device Control 1 (XON)',
    'Device Control 2','Device Control 3 (XOFF)','Device Control 4',
    'Negative Acknowledgement','Synchronous idle','End of Transmission Block',
    'Cancel','End of Medium','Substitute','Escape','File Separator',
    'Group Separator','Record Separator','Unit Separator')
    t=[[str(i),hex(i),chr(i)] for i in xrange(256)]
    for i in xrange(32): t[i][2]=a[i]
    f=open('ascii.txt','w')
    for i in xrange(16):
        f.write('{0:5},{1:6},{2:24} '.format(*t[i]))
        f.write('{0:5},{1:6},{2:24}'.format(*t[i+16])+'\n')
    f.write(78*'-'+'\n')
    for i in xrange(32,56):
        f.write('{0:4},{1:4},{2:>6} | '.format(*t[i]))
        f.write('{0:4},{1:4},{2:>6} | '.format(*t[i+24]))
        f.write('{0:4},{1:4},{2:>6} | '.format(*t[i+2*24]))
        f.write('{0:4},{1:4},{2:>6}'.format(*t[i+3*24])+'\n')
    f.write(78*'-'+'\n')
    for i in xrange(128,160):
        f.write('{0:4},{1:4},{2:>6} | '.format(*t[i]))
        f.write('{0:4},{1:4},{2:>6} | '.format(*t[i+32]))
        f.write('{0:4},{1:4},{2:>6} | '.format(*t[i+2*32]))
        f.write('{0:4},{1:4},{2:>6}'.format(*t[i+3*32])+'\n')
    f.close()

IMN logo majestic logo threadwatch logo seochat tools logo