[Tutor] map one file and print it out following the sequence

Andreas Perstinger andreas.perstinger at gmx.net
Wed Oct 12 09:29:40 CEST 2011

On 2011-10-12 05:31, lina wrote:
> I tried to write one (not working one) as below, so many problems here.

Just some quick remarks:

> #!/usr/bin/python3
> import os.path
> INFILENAME="pdbone.pdb"
> DICTIONARYFILE="itpone.itp"
> mapping={}
> valuefromdict={}
> def sortfile():
>      for chainid in CHAINID:
>          sortoneblock(chainid)
> def generatedictionary(dictfilename):

You define the function with the parameter "dictfilename" but you'll 
never use it.

>      text=fetchonefiledata(DICTIONARYFILE)
>      for line in text:
>          parts=line.strip().split()
>      if len(parts)==8:
>          mapping[parts[4]]=parts[0]
>          print(mapping)

The if-branch is probably wrongly indented (should be inside the for-loop).

> def sortoneblock(cID)
>      text=fetchonefiledata(INFILENAME)
>      for line in text:
>          blocks=line.strip().split()
>          if len(blocks)== 11 and  blocks[3] == "CUR" and blocks[4] == "cID":

"cID" is a string-variable but you compare block 4 to the literal string 
"cID". In "pdbone.pdb" you will never find "cID" so this function will 
do nothing. You probably mean "blocks[4] == cID".

>              valuefromdict[blocks[2]]=mapping[block[2]]

You never fill up "mapping" because you never call your 
"generatedictionary"-function. Therefore "mapping" is still an empty 
dictionary and this line will raise an exception.

>              return

Firstly, the indentation is wrong because you would leave "sortoneblock" 
after the first processed line. Secondly, as you return nothing, you 
don't need this line because you will leave the function anyway.

> def fetchonefiledata(infilename):
>      text=open("infilename").readlines()

Again, "infilename" is a variable, so no need for the quotes.

>      if os.path.splitext(infilename)[1]=".itp"
>          return text
>      if os.path.splitext(infilename)[1]=".pdb"
>          return text[LINESTOSKIP:]
> if __name__=="__main__":
>      sortfiles()

There is no "sortfiles()" in your script. The function you probably mean 
is called "sortfile()"

Bye, Andreas

More information about the Tutor mailing list