[Tutor] map one file and print it out following the sequence
Andreas Perstinger
andreas.perstinger at gmx.net
Thu Oct 13 16:52:20 CEST 2011
On 2011-10-13 15:09, lina wrote:
> $ 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.
You should start to learn how to read the error messages :-).
"write" just writes strings into files ("must be str") but you are
calling it with an generator. I guess you wanted to use a list
comprehension, but this is surrounded by square brackets:
f.write([line[1].strip() for line in temp])
But list comprehensions create lists so you would have to convert the
list to a string:
f.write(str([line[1].strip() for line in temp]))
But this would convert the whole list into one single string which you
probably don't want (try it for yourself).
IMHO it would be easier to iterate through "temp" and write each line
separately:
for line in temp:
f.write(line[1])
"line[1]" is already a string including the newline ("\n"), so str() and
strip() aren't necessary (remeber: "write" writes without automatic
newlines).
Is that what you want?
> can I write the different chainID one into the same OUTFILE?
I'm not sure what you mean. Do you want something like:
C
xxxxxxxx
xxxxxxxx
xxxxxxxx
D
xxxxxxx
xxxxxxx
...
("xxxxxxx" meaning the different lines)?
Then you just have to write the corresponding chainID before the for-loop:
f.write(cID + "\n")
And you have to open the file in mode "a" (to append to an existing
file) because otherwise you will overwrite the file with every new
chainID you are processing:
with open(OUTFILE, "a") as f:
> def sortoneblock(cID,TEXT,OUTFILE):
^^^^^^^^^^^^
Just a stylistic remark: It's better to use just lowercase for variable
names and parameters. Uppercase names are usually just used for
constants. Thus it's easier to distinguish them while reading the code.
Bye, Andreas
More information about the Tutor
mailing list