Re: [Tutor] creating a regularly placed fields in a line
On Wed, 25 Apr 2012, Prasad, Ramit wrote: Useful to know both though, since lots of people swear by % substitution. And % formatting is slightly faster - if you end out doing tons of formatting and you find your script isn't fast enough, it's worth taking a look there. -Wayne ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creating a regularly placed fields in a line
On 25/04/12 16:17, Bala subramanian wrote: placed depending up on the size of the field. I am thinking to fix the size of each field. with open('tmp') as tp: for line in tp: ... bond.write(' %s\t%s\t%s\t%s\t%s\n' % (p1,p2,p3,p4,new) ) Add your widths to the format string: bond.write(' %10s%20s%30s%10s%25s\n' % (p1,p2,p3,p4,new) ) adjust the lengths to suit. if you write the data in several places you can ensure consistency by defining the format string as a variable: fmt = ' %10s%20s%30s%10s%25s\n' bond.write( fmt % (p1,p2,p3,p4,new) ) hth, -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creating a regularly placed fields in a line
> > > > Not really sure how to do the equivalent with % substitution. > > > > See > http://docs.python.org/library/stdtypes.html#string-formatting-operations > > Mark Lawrence. Thanks Mark. Based on that, I find .format more intuitive (especially for alignment) than % substitution. Useful to know both though, since lots of people swear by % substitution. Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216 - 5423 -- This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creating a regularly placed fields in a line
On 25/04/2012 16:57, Prasad, Ramit wrote: Not really sure how to do the equivalent with % substitution. Ramit See http://docs.python.org/library/stdtypes.html#string-formatting-operations -- Cheers. Mark Lawrence. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creating a regularly placed fields in a line
> I wrote a small piece of code (given below). The aim is to take each line > in a file, split the fields, replace the first four fields and then write > the new lines in a output file. The input and output are given in the > attached file. The output contains fields which are irregularly placed > depending up on the size of the field. I am thinking to fix the size of > each field. Kindly provide me some way on how i can do the same or a > better way to fix placement of fields. > > with open('tmp') as tp: > for line in tp: > line=line.split() > p1=atm_type[line[0]];p2=atm_type[line[1]] > p3=atm_type[line[2]];p4=atm_type[line[3]] > new='\t'.join(line[4:10]) > bond.write(' %s\t%s\t%s\t%s\t%s\n' % (p1,p2,p3,p4,new) ) Taking a look at your output file leads me think that it is not really meant to be human readable. If it is only for a program to use, then I would just make it comma separated using the csv module. If you are intending to make it readable to humans, I would suggest looking at string formatting. It is better than tabbing since most clients show tabs a little differently and a tab may still cause irregular columns if you have a large difference in each row's column size. The formatting mini-language can be a little complex but luckily the guide is useful. http://docs.python.org/library/string.html#format-specification-mini-language The examples on that page are useful and I think what you want is quoted below. " Aligning the text and specifying a width: >>> '{:<30}'.format('left aligned') 'left aligned ' >>> '{:>30}'.format('right aligned') ' right aligned' >>> '{:^30}'.format('centered') ' centered ' >>> '{:*^30}'.format('centered') # use '*' as a fill char '***centered***' " Not really sure how to do the equivalent with % substitution. Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216 - 5423 -- This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] creating a regularly placed fields in a line
Friends, I wrote a small piece of code (given below). The aim is to take each line in a file, split the fields, replace the first four fields and then write the new lines in a output file. The input and output are given in the attached file. The output contains fields which are irregularly placed depending up on the size of the field. I am thinking to fix the size of each field. Kindly provide me some way on how i can do the same or a better way to fix placement of fields. with open('tmp') as tp: for line in tp: line=line.split() p1=atm_type[line[0]];p2=atm_type[line[1]] p3=atm_type[line[2]];p4=atm_type[line[3]] new='\t'.join(line[4:10]) bond.write(' %s\t%s\t%s\t%s\t%s\n' % (p1,p2,p3,p4,new) ) -- C. Balasubramanian test.output Description: Binary data ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor