Pythonでアスキーコード0x20から0X7Eの文字をファイルに出力する

文字から文字コードを取得する

>>> ord('a')
97
>>> ord('z')
122
>>> ord('A')
65
>>> ord('Z')
90
>>> ord('0')
48
>>> ord('9')
57
>>> ord(' ')
32
>>> ord('~')
126

16進数で表示するなら

>>> hex(ord('a'))
'0x61'
>>> hex(ord(' '))
'0x20'
>>> hex(ord('~'))
'0x7e'

文字コードを文字に変換する

>>> chr(97)
'a'
>>> chr(0x61)
'a'
>>> chr(0x20)
' '
>>> chr(0x7e)
'~'

アスキーコード0x20から0X7Eの文字をファイルに出力する

f = open('ascii.txt', 'w')
for code in range(0x20, 0x7e + 1):
    f.write(chr(code))
f.close()
$ cat ascii.txt 
 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~

$ od -tx1 ascii.txt 
0000000 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f
0000020 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f
0000040 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f
0000060 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f
0000100 60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f
0000120 70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e