1

Trying python after a very long time now.

I have a file that has a line:

My_NUMBER                 =  24

I want to extract this number ( here I have assumed 24 but this is something I want to extract based on My_NUMBER.In my python code I am able to read the line

with open(filename) as f: lines = f.read().splitlines()

for line in lines:
    if line.startswith(' My_NUMBER                 ='):
        line=(line.rsplit(' ', 1)[0])
        num= line.rsplit('=',1)[1].split("=")[0]
        num = num.strip(" ")
        print num

However this does print blank output and not the number. Can anyone commet in case I am doing anything obviously wrong here?

user741592
  • 875
  • 3
  • 10
  • 25

7 Answers7

3

This is the perfect job for a regex:

import re
text = open(filename).read()
print re.search("^\s*My_NUMBER\s*=\s*(\d*)\s*$", text, re.MULTILINE).group(1)
enrico.bacis
  • 30,497
  • 10
  • 86
  • 115
2

I'd go with something like this

with open(filename) as f:
    for line in f:
        line = line.replace(' ', '')
        if line.startswith('My_NUMBER'):
            number = line.partition('=')[2]
            print number
Tim
  • 11,710
  • 4
  • 42
  • 43
  • 2
    Why has this been downvoted? It might not be perfect, but it's not as ghastly as all this regex answers. – SilentGhost Oct 31 '12 at 12:54
  • This will remove whitespace from the unknown value after the `=` - better to use `.strip()` on the `str.partition`'d value – dbr Oct 31 '12 at 13:09
  • could you add [code showing the OP how to read a file line-by-line](http://ideone.com/66bnWb) otherwise if the whole file is already in memory then a regex would work fine there is no need to iterate line by line – jfs Oct 31 '12 at 13:10
  • @dbr: there is no whitespace in a number – jfs Oct 31 '12 at 13:10
0

Might be better off using regex

import re

txt='My_NUMBER                 =  24'

re1='.*?'   # Non-greedy match on filler
re2='(\\d+)'    # Integer Number 1

rg = re.compile(re1+re2,re.IGNORECASE|re.DOTALL)
m = rg.search(txt)
if m:
    int1=m.group(1)
    print "("+int1+")"+"\n"

From here: http://txt2re.com/index-python.php3?s=My_NUMBER%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20=%20%2024&4

Paul Collingwood
  • 9,053
  • 3
  • 23
  • 36
0

try something like this:

In [49]: with open("data1.txt") as f:
   ....:     for line in f:
   ....:         if line.strip():           #if line is not an empty line
   ....:             if "My_NUMBER" in line:
   ....:                 num=line.split("=")[-1] # split line at "=" and 
                                                 # return the last element
   ....:                 print num
   ....:                 
   ....:                 
  24
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
0
for line in lines:
    if line.startswith(' My_NUMBER                 ='):
        num = line.split('=')[-1].strip()
        print num
iurisilvio
  • 4,868
  • 1
  • 30
  • 36
  • 5
    This indentation looks funny to me. I'm pretty sure you need to move `lines = f.read().splitlines()`. Also, if you're going to do that, you might as well get rid of it all together and just iterate over the file: `for line in f:` instead of `for line in lines:` – mgilson Oct 31 '12 at 12:53
  • @mgilson yeap, that was completely wrong. Sorry. At least, it helped the OP. – iurisilvio Oct 31 '12 at 17:16
0
import re

exp = re.compile(r'My_NUMBER *= *(\d*)')
with open('infile.txt','r') as f:
    for line in f:
        a = exp.match(line)
        if a:
            print a.groups()[0]

Untested, but should work

Matt
  • 3,651
  • 3
  • 16
  • 35
0

The tidiest way, I would say, is to loop over each line, split based on =, then check if the value before the = is MY_NUMBER

The str.partition function is good for this (it's similar to split, but always returns 3 chunks). Also using str.strip means you don't need to worry about the whitespace

with open(filename) as f:
    lines = f.readlines()

for line in lines:
    key, eq, value = line.partition("=")
    if key.strip() == "MY_NUMBER":
        num = value.strip()
        print num
dbr
  • 165,801
  • 69
  • 278
  • 343
  • 1
    Besides being nearly identical to Tim's answer. Why, oh why, are you doing `.read().splitlines()`? – SilentGhost Oct 31 '12 at 12:56
  • @SilentGhost it was copy-pasted from the original answer, edited (Tim's answer is also not identical, depending on the value after the `=`, commented on this answer) – dbr Oct 31 '12 at 13:07