[Tutor] how to get blank value

Dave Angel davea at ieee.org
Tue Jul 28 23:26:10 CEST 2009


amrita at iisermohali.ac.in wrote:
> It is not giving any value, without any error
>
> ph08001 at sys53:~> python trial.py
> ph08001 at sys53:~>
> it is coming out from shell.
>
> Thanks for help.
> Amrita
>
>   
>> amrita at iisermohali.ac.in wrote:
>>     
>>> Sorry to say, but till now I have not got the solution of my problem, I
>>> tried with this command:-
>>>
>>> import re
>>>
>>>
>>>       
>> # assuming H = , N = , CA = , HA =  and C = always present in that order
>> if __name__ == '__main__':
>>   data = open('chem.txt').readlines()
>>   for line in data:
>>     line = line.split('=')
>>     if not line[5]: # C value missing
>>      if len(line[2])==1 and len(line[3])==1: # N and CA values missing
>>         print "all missing", line
>>       else:
>>         print "C missing", line
>>
>>     
>>>       
>>>> Hi,
>>>>
>>>> I have a file having lines:-
>>>>
>>>> 48 ALA H = 8.33 N = 120.77 CA = 55.18 HA = 4.12 C = 181.50
>>>> 104 ALA H = 7.70 N = 121.21 CA = 54.32 HA = 4.21 C =
>>>> 85 ALA H = 8.60 N =  CA =  HA = 4.65 C =
>>>>
>>>> Now i want to make two another file in which i want to put those lines
>>>> for
>>>> which C is missing and another one for which N,CA and C all are
>>>> missing,
>>>>
>>>> With these commands:-
>>>>
>>>> import re
>>>> f = open('chem.txt')
>>>> for line in f:
>>>>      if re.search('C = ',line):
>>>>         print line
>>>>
>>>> I am getting those lines for which C value is there but how to get
>>>> those
>>>> one for which it doesn't have any value, i did google search but still
>>>> i
>>>> am not getting.
>>>>
>>>> Amrita Kumari
>>>> Research Fellow
>>>> IISER Mohali
>>>> Chandigarh
>>>> INDIA
>>>>
>>>>
>>>>
>>>>         
>> --
>> Bob Gailer
>> Chapel Hill NC
>> 919-636-4239
>>
>>     
>
>
> Amrita Kumari
> Research Fellow
> IISER Mohali
> Chandigarh
> INDIA
>
>
>   
If you add a strip() ,method call to Bob's line2 definition, it should 
work with your data, when read from file.

       line2 = line.strip().split('=')


But you should realize his code assumes the data fits rather rigid 
rules.  Before you use such code, you should make sure the data will fit 
it correctly.

For example, a blank line will crash the loop.  A line with the four 
variables in some other order will not work.  A fifth variable on a line 
will make it not work.

DaveA



More information about the Tutor mailing list