<snip> > I think your final version of sortfile() might look something like: > > def sortfile(infilename=**INFILENAME, outfilename=OUTFILENAME): > infile = open(infilename, "r") > intext = infile.readlines() > outfile = open(OUTFILENAME, "w") > for chainid in CHAINID: > print("chain id = ",chainid) > sortoneblock(chainid, intext, outfile) > infile.close() > outfile.close() >
$ python3 map-to-itp.py {'O4': '2', 'C19': '3', 'C21': '1'} C Traceback (most recent call last): File "map-to-itp.py", line 55, in <module> sortfile() File "map-to-itp.py", line 17, in sortfile sortoneblock(chainid,intext,OUTFILENAME) File "map-to-itp.py", line 29, in sortoneblock f.write(line[1].strip() for line in temp) TypeError: must be str, not generator I don't know how to fix the writing issue. can I write the different chainID one into the same OUTFILE? Thanks, I attached the code I used below: #!/usr/bin/python3 import os.path LINESTOSKIP=0 CHAINID="CDEFGHI" INFILENAME="pdbone.pdb" OUTFILENAME="sortedone.pdb" DICTIONARYFILE="itpone.itp" mapping={} valuefromdict={} def sortfile(): intext=fetchonefiledata(INFILENAME) for chainid in CHAINID: print(chainid) sortoneblock(chainid,intext,OUTFILENAME) def sortoneblock(cID,TEXT,OUTFILE): temp = [] for line in TEXT: blocks=line.strip().split() if len(blocks)== 11 and blocks[3] == "CUR" and blocks[4] == cID and blocks[2] in mapping.keys(): temp.append((mapping[blocks[2]],line)) temp.sort() with open(OUTFILE,"w") as f: f.write(line[1].strip() for line in temp) def generatedictionary(dictfilename): text=fetchonefiledata(DICTIONARYFILE) for line in text: parts=line.strip().split() if len(parts)==8: mapping[parts[4]]=parts[0] print(mapping) def fetchonefiledata(infilename): text=open(infilename).readlines() if os.path.splitext(infilename)[1]==".itp": return text if os.path.splitext(infilename)[1]==".pdb": return text[LINESTOSKIP:] infilename.close() if __name__=="__main__": generatedictionary(DICTIONARYFILE) sortfile() > > > -- > > DaveA > >
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor