Re: extracting numbers from a file, excluding words
On Nov 01, Mike Meyer wrote: > Kristina Kudriaðova <[EMAIL PROTECTED]> writes: > > > 1 Nov 2005 09:19:45 -0800, [EMAIL PROTECTED] <[EMAIL PROTECTED]>: > >> Hi, I have a file with this content: > >> > >> z zzz z > >> ... > >> xxx xx x 34.215 > >> zzz zz > >> ... > >> > > > > Hi, > > > > I'd suggest doing this: > > > > f = file('...') > > for line in f: > > if 'xxx xx x' in line: > > var = float(line[len('xxx xx x'):].strip()) > > f.close() > > Alternatively: > > start = len('xxx xx x') > for line in f: > if line.startswith('xxx xx x'): >var = float(line[start:].strip()) To refine this even further, I'll add that 'xxx...' is an ugly pattern to repeat, and prone to mistyping, so add a tempvar and apply DRY:: pattern = 'xxx xx x' start = len(pattern) for line in f: if line.startswith(pattern): var = float(line[start:].strip()) -- _ _ ___ |V|icah |- lliott http://micah.elliott.name [EMAIL PROTECTED] " " """ -- http://mail.python.org/mailman/listinfo/python-list
Re: extracting numbers from a file, excluding words
Kristina Kudriaðova <[EMAIL PROTECTED]> writes: > 1 Nov 2005 09:19:45 -0800, [EMAIL PROTECTED] <[EMAIL PROTECTED]>: >> Hi, I have a file with this content: >> >> z zzz z >> ... >> xxx xx x 34.215 >> zzz zz >> ... >> > > Hi, > > I'd suggest doing this: > > f = file('...') > for line in f: > if 'xxx xx x' in line: > var = float(line[len('xxx xx x'):].strip()) > f.close() Alternatively: start = len('xxx xx x') for line in f: if line.startswith('xxx xx x'): var = float(line[start:].strip()) http://www.mired.org/home/mwm/ Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information. -- http://mail.python.org/mailman/listinfo/python-list
Re: extracting numbers from a file, excluding words
Steve Horsley wrote: > Kristina Kudriašova wrote: > >> 1 Nov 2005 09:19:45 -0800, [EMAIL PROTECTED] <[EMAIL PROTECTED]>: >> >>> Hi, I have a file with this content: >>> >>> z zzz z >>> ... >>> xxx xx x 34.215 >>> zzz zz >>> ... >>> >> >> Hi, >> >> I'd suggest doing this: >> >> f = file('...') >> for line in f: >> if 'xxx xx x' in line: >> var = float(line[len('xxx xx x'):].strip()) >> f.close() > > > I think I prefer "if line.startswith('xxx xx x'):" . > Feels cleaner to me. Especially if any "z" lines might include the magic pattern. -- http://mail.python.org/mailman/listinfo/python-list
Re: extracting numbers from a file, excluding words
Kristina Kudriašova wrote: > 1 Nov 2005 09:19:45 -0800, [EMAIL PROTECTED] <[EMAIL PROTECTED]>: >> Hi, I have a file with this content: >> >> z zzz z >> ... >> xxx xx x 34.215 >> zzz zz >> ... >> > > Hi, > > I'd suggest doing this: > > f = file('...') > for line in f: > if 'xxx xx x' in line: > var = float(line[len('xxx xx x'):].strip()) > f.close() I think I prefer "if line.startswith('xxx xx x'):" . Feels cleaner to me. Steve -- http://mail.python.org/mailman/listinfo/python-list
Re: extracting numbers from a file, excluding words
1 Nov 2005 09:19:45 -0800, [EMAIL PROTECTED] <[EMAIL PROTECTED]>: > Hi, I have a file with this content: > > z zzz z > ... > xxx xx x 34.215 > zzz zz > ... > Hi, I'd suggest doing this: f = file('...') for line in f: if 'xxx xx x' in line: var = float(line[len('xxx xx x'):].strip()) f.close() -- http://mail.python.org/mailman/listinfo/python-list
extracting numbers from a file, excluding words
Hi, I have a file with this content: z zzz z ... xxx xx x 34.215 zzz zz ... "x" and "z" are letters. The lines with "z" are trash, and only the lines with "x" are important. I want to extract the number (34.215 in this case) behind the letters x, and store it in a file. The sentence "xxx xx x " is FIXED and KNOWN. The "z" sentences are can vary. There are also unknown number of "z" lines. Any suggestions will be appreciated. -- http://mail.python.org/mailman/listinfo/python-list