#Python Standard Library #Output Formatting import reprlib result=reprlib.repr(set('supercallingfragmentdocious')) print(result) #:set(['a', 'c', 'd', 'e', 'f', 'g', ...]) import pprint t=[[[['black', 'cyan'], 'white', ['green', 'red']], [['magenta','yellow'], 'blue']]] pprint.pprint(t,width=30) #:[[[['black', 'cyan'], #: 'white', #: ['green', 'red']], #: [['magenta', 'yellow'], #: 'blue']]] #The textwrap module formats paragraphs of text to fit a given screen width: import textwrap doc="""The wrap() method is just like fill() excep it returns a list of strings instead""" print(textwrap.fill(doc,width=20)) #:The wrap() method is #:just like fill() #:excep it returns a #:list of strings #:instead #The locale module accesses a database of culture specific data formats. The grouping attribute of locale's format function provides #a direct way of formatting numbers with group separators import locale locale.setlocale(locale.LC_ALL,'English_United States.1252') conv=locale.localeconv x=1234567.8 result=locale.format("%d",x,grouping=True) print(result) #result=locale.format("%s%.*f", (conv['currency_symbol'],conv['frac_digits'], x), grouping=True) #print(result) #Templating from string import Template t=Template('${village}folk send $10 to $cause.') result=t.substitute(village='Beijing',cause='the ditch fund') print(result) t=Template('Retutn the $item to $owner.') d=dict(item='unladen swallow') result=t.safe_substitute(d) #if not fund it will leave placeholder unchanged print(result) import time,os.path photofiles=['img_1074.jpg','img_1076.jpg','img_1077.jpg'] class BatchRename(Template): delimiter='%' fmt=input('Enter rename style (%d-date %n-seqnum %f-format):') t=BatchRename(fmt) date=time.strftime('%d%b%y') for i,filename in enumerate(photofiles): base,ext=os.path.splitext(filename) newname=t.substitute(d=date,n=i,f=ext) print('{0} ---> {1}'.format(filename,newname)) #Working with Binary Data Record Layouts import struct data=open('StandLib1.zip').read() start=0 for i in range(3): start+=4 fields=struct.unpack('<IIIHH',data[strat:strat+16]) crc32,comp_size,uncomp_size,filenamesize,extra_size=fields start+=16 filename=data[start:start+filenamesize] start+=filenamesize extra=data[start:start+extra_size] print(filename,hex(crc32),comp_size,uncomp_size) stzrt+=extra_size+comp_size