how to avoid truncation of numbers- Python -


i have text file, split parts 10 lines each using python. trying split them 4 digit suffixes instead of 1,2,3,etc. example, if file filename.txt, file must split into, filepart-0000.txt, filepart-0001.txt , on. right now, if have:

suffix = 0000

and try write filepart as: open ("filepart-" + str(suffix),'w')

i still end getting: filepart-0.txt, filepart-1.txt , on.

if straightaway write: open ("filepart-" + suffix, 'w')

i (obviously) end 'typeerror:cannot concatenate 'str' , 'int' objects' there way can retain entire suffix of 4 digits? in advance.

you try using string formatting 0 padding follows:

in [1]: '{:04}'.format(1) out[1]: '0001'  in [2]: '{:04}'.format(23) out[2]: '0023' 

the idea can provide target integers would, pass them in argument format in order pad digit 0s contains @ least 4 digits. can use string in filename.

in [3]: in xrange(1, 11):    ...:     print 'filename-{:04}.txt'.format(i)    ...:      filename-0001.txt filename-0002.txt filename-0003.txt filename-0004.txt filename-0005.txt filename-0006.txt filename-0007.txt filename-0008.txt filename-0009.txt filename-0010.txt 

Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

C# WinForm - loading screen -