Re: Line indexing in Python

2009-12-22 Thread MRAB
Lie Ryan wrote: On 12/22/2009 11:25 PM, Steve Holden wrote: > > If you want to extract an index number from the first part of of a given > line use split( split_character, maximum_splits_to_do ) and then angle > brackets to reference the first part (index 0)... > > a = "20 GOTO 10"

Re: Line indexing in Python

2009-12-22 Thread r0g
Steve Holden wrote: > r0g wrote: >> seafoid wrote: >>> Hi Guys, >>> >>> When python reads in a file, can lines be referred to via an index? >>> >>> Example: >>> >>> for line in file: >>> if line[0] == '0': >>> a.write(line) >>> >>> This works, however, I am unsure if line[0] refers on

Re: Line indexing in Python

2009-12-22 Thread Lie Ryan
On 12/22/2009 11:25 PM, Steve Holden wrote: > > If you want to extract an index number from the first part of of a given > line use split( split_character, maximum_splits_to_do ) and then angle > brackets to reference the first part (index 0)... > > a = "20 GOTO 10" int( a.split('

Re: Line indexing in Python

2009-12-22 Thread Steve Holden
r0g wrote: > seafoid wrote: >> Hi Guys, >> >> When python reads in a file, can lines be referred to via an index? >> >> Example: >> >> for line in file: >> if line[0] == '0': >> a.write(line) >> >> This works, however, I am unsure if line[0] refers only to the first line or >> the fir

Re: Line indexing in Python

2009-12-21 Thread r0g
seafoid wrote: > Hi Guys, > > When python reads in a file, can lines be referred to via an index? > > Example: > > for line in file: > if line[0] == '0': > a.write(line) > > This works, however, I am unsure if line[0] refers only to the first line or > the first character in all l

Re: Line indexing in Python

2009-12-18 Thread seafoid
Rory, You are a gentleman! Thank you very much for your suggestion! Kind Regards, Seafoid. Rory Campbell-Lange wrote: > > On 18/12/09, seafoid (fitzp...@tcd.ie) wrote: >> http://old.nabble.com/Parsing-file-format-to-ensure-file-meets-criteria-to26837682.html > > Your specification is confus

Re: Line indexing in Python

2009-12-18 Thread seafoid
Hey folks, Is it possible to assign a list within a nested list to a variable? Example: l = [['1', '2', '3'], ['4', '5', '6']] for i in l: if i[0][1] == '1': m = i Indeed, I generally do not understand how to assign variables within a loop! Is there an easy way to 'flatten' a nes

Re: Line indexing in Python

2009-12-18 Thread Rory Campbell-Lange
On 18/12/09, seafoid (fitzp...@tcd.ie) wrote: > http://old.nabble.com/Parsing-file-format-to-ensure-file-meets-criteria-to26837682.html Your specification is confusing. However I suggest you break it down the code so that the steps in your programme are logical. Good luck. # example psuedocode he

Re: Line indexing in Python

2009-12-18 Thread Rory Campbell-Lange
On 18/12/09, seafoid (fitzp...@tcd.ie) wrote: > Have you any suggestions how I may render this code undead or should I scrap > it and create something new? It might be easier for us to help you if you give us an example of your input file and a clearer description of what you are trying to do with

Re: Line indexing in Python

2009-12-18 Thread seafoid
Hi Guys, It has been point out that it is difficult for anyone to provide suggestions if I do not outline more clearly my input file and an example of what I wish to do with it (Thanks Rory!). I mentioned it in this thread (Is creating different threads bad etiquette? If so, lesson learned!): h

Re: Line indexing in Python

2009-12-18 Thread Lie Ryan
On 12/19/2009 4:33 AM, seafoid wrote: Thanks for that Lie. I had to have a think about what you meant when you referred to control going to a.write(line). and if-elif-elif-... chain is executed sequentially and when a match is found, the rest of the chain is skipped. Your code: if line.sta

Re: Line indexing in Python

2009-12-18 Thread seafoid
Thanks for that Lie. I had to have a think about what you meant when you referred to control going to a.write(line). Have you any suggestions how I may render this code undead or should I scrap it and create something new? My confusion and ineptitude is perhaps explained by my being a biologist

Re: Line indexing in Python

2009-12-18 Thread Lie Ryan
On 12/19/2009 3:27 AM, seafoid wrote: Thanks for that Richard and Steve. I have another question. What's the question? fname = raw_input('Please enter the name of the file: ') # create file objects blah = open(fname, 'r') a = open('rubbish', 'w') for line in blah: if line.startswith

Re: Line indexing in Python

2009-12-18 Thread seafoid
Thanks for that Richard and Steve! Below is my full code so far: for line in file: if line.startswith("1"): a.write(line) elif line.endswith("0"): lists_a = line.strip().split() print lists_a elif line.startswith("2"): lists_b = line.strip().split()

Re: Line indexing in Python

2009-12-18 Thread seafoid
Thanks for that Richard and Steve. I have another question. fname = raw_input('Please enter the name of the file: ') # create file objects blah = open(fname, 'r') a = open('rubbish', 'w') for line in blah: if line.startswith("0"): a.write(line) elif line.endswith("0"):

Re: Line indexing in Python

2009-12-18 Thread Steve Holden
seafoid wrote: > Hi Guys, > > When python reads in a file, can lines be referred to via an index? > > Example: > > for line in file: > if line[0] == '0': > a.write(line) > > This works, however, I am unsure if line[0] refers only to the first line or > the first character in all l

Re: Line indexing in Python

2009-12-18 Thread Richard Thomas
On Dec 18, 3:42 pm, seafoid wrote: > Hi Guys, > > When python reads in a file, can lines be referred to via an index? > > Example: > > for line in file: >      if line[0] == '0': >          a.write(line) > > This works, however, I am unsure if line[0] refers only to the first line or > the first c