トップ 差分 一覧 ソース 検索 ヘルプ RSS ログイン

tips-PYTHON-fdump

python3 でファイルをダンプ

#!/bin/env python3

import sys, string

# DATALEN = 376
DATALEN = 380
# DATALEN = 384

with open('UNKNWN.DAT', 'rb') as f:
   while True:
       for i in range(DATALEN): 
           d = f.read(1)
           n = ord(d)
           if len(d) == 0:
             break
           if ( i < 12 ):
               ## print('%s (%x) ' % (ord(d), ord(d)), end='')
               # if n in string.printable[:-5]: # without \t,\n,\x,0b, \x, 0c, \r
               # if d in string.printable[:-5]: # without \t,\n,\x,0b, \x, 0c, \r
               #
               # print('(%2x) ' %  ord(d), end='')
               if ( n >  0x7f ):
                       print ('. ' , end ='' )
               elif d.decode(encoding='utf-8') in string.printable[:-5]:
                       # without \t,\n,\x,0b, \x, 0c, \r
                       print ('%s ' % d.decode() , end ='' )
                       # print ('%s ' % d , end ='' )
               else:
                   print ('. ' , end ='' )
               #
               print('(%2x) ' %  ord(d), end='')
               # sys.stdout.write('.')
       print('====')