0

I wanted parse a file to search one word and print next line i have written python script as follows

infile = open("s.sdf","r")
output = open("sample.txt","w")
d = None
HD = None
HA = None
M = None

for line in infile:
      if line.startswith(">  <PUBCHEM_COMPOUND_CID>"):
         d = infile.next().strip()
         print d      
      elif line.startswith(">  <PUBCHEM_CACTVS_HBOND_DONOR>"):
         HD = infile.next().strip()
         print HD
      elif line.startswith(">  <PUBCHEM_CACTVS_HBOND_ACCEPTOR>"):
         HA = infile.next().strip()
         print HA
      elif line.startswith(">  <PUBCHEM_MOLECULAR_WEIGHT>"):
         M = infile.next().strip()
         print M

      print "%s \t  %s  \t  %s  \t  %s" %(d,HD,HA,M)
      output.write("%s  \t  %s  \%s \t  %s" %(d,HD,HA,M))

But unfortunately i getting an error as follows

None None None None

None None None None

None None None None

None None None None
.......

Can anybody tell me how to solve this..

Thanks in Advance

N

6
  • Please use the {} button in the SO editor to format your code (or indent everything 4 spaces). I fixed it for you. Commented Aug 19, 2011 at 21:54
  • Well, that probably means that none of your startswith conditions are matching. Post some of your data. Also, add some print statements under the conditions so you can see when they are getting triggered (if they are) -- debugging 101. Commented Aug 19, 2011 at 21:54
  • 1
    Looks like XML, use a python XML processor instead. There are thousands of questions and answers on this topic on SO. Commented Aug 19, 2011 at 21:56
  • There is no such think like declaration in Python. New variable is simply new value in your namespace local dictionary. Commented Aug 19, 2011 at 22:05
  • 2
    @MattH, it's not XML. OP's code references an .sdf extension, which turns out to be a chemical data file. It uses angle brackets, but only to identify header lines. en.wikipedia.org/wiki/SD_format#SDF Commented Aug 19, 2011 at 22:25

1 Answer 1

1

To skip lines that do not match those strings add a check:

if any(bool(x) for x in d, HD, HA, M):
    print ...
    output.write

Try running the script in a debugger:

$ python -m pdb your_script.py

and see what variables are there and what's wrong. Since PDB is not convenient, you might want to install ipdb or pudb.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.