[Tutor] print problem

Hs Hs ilhs_hs at yahoo.com
Sat Dec 4 01:27:59 CET 2010


hi I have a file and it is in chunks:
I want to be able to select the number that follows 'Elution: '  where the line 
startswith (TITLE)(say
72.958) and check if it is larger than my choice of number (say
71.4). If it is then I want to print chunk from BEGIN LINE to END
LINE separated by one empty line.

I wrote the following script, when executed in IDEL or python mode, it works. 
however I use python test_script_test.py , I get name error:

could you please help me whats wrong here. 
thanks


Command line mode:

$ python test_script_test.py
Traceback (most recent call last):
  File "test_script_test.py", line 14, in <module>
    newk = dat[mystartl:myfinish]
NameError: name 'myfinish' is not defined


In python mode:
>>> f1 = open('test','r')
>>> da = f1.read().split('\n')
>>> dat = da[:-1]
>>> mytimep = 71.4
>>> for i in range(len(dat)):
...         if dat[i].startswith('BEGIN LINE'):
...                 mystartl = i
...         if dat[i].startswith('END LINE'):
...                 myfinish = i
...         if dat[i].startswith('Title'):
...                 col = dat[i].split(',')
...                 x= float(col[2].split()[1])
...                 if x > mytimep:
...                         newk = dat[mystartl:myfinish]
...                         for x in newk:
...                                 print x
...
BEGIN LINE
MYMASS=643.973587067287
CHARGE=2+
Title=File:xyx, sample= Cyst 7 band 7 (sample number 8), Elution: 72.958 to 
73.198 min, Period: 1
132.4082 0.0323
133.0418 0.2596
136.0618 0.3276
138.0482 0.546
140.4848 0.0499
140.7096 0.0333
END LINE

BEGIN LINE
MYMASS=643.973587067287
CHARGE=2+
Title=File:xyx, sample= Cyst 7 band 7 (sample number 8), Elution: 72.958 to 
73.198 min, Period: 1
132.4082 0.0323
133.0418 0.2596
136.0618 0.3276
138.0482 0.546
140.4848 0.0499
140.7096 0.0333
















my file:

BEGIN LINE
MYMASS=643.973587067287
CHARGE=2+
Title=File:xyx, sample= Cyst 7 band 7 (sample number 8), Elution: 72.958 to 
73.198 min, Period: 1
132.4082 0.0323
133.0418 0.2596
136.0618 0.3276
138.0482 0.546
140.4848 0.0499
140.7096 0.0333
END LINE

BEGIN LINE
MYMASS=643.973587067287
CHARGE=2+
Title=File:xyx, sample= Cyst 7 band 7 (sample number 8), Elution: 72.958 to 
73.198 min, Period: 1
132.4082 0.0323
133.0418 0.2596
136.0618 0.3276
138.0482 0.546
140.4848 0.0499
140.7096 0.0333
END LINE


      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20101203/18eef47a/attachment.html>


More information about the Tutor mailing list