0

Hi,

I'm trying to create a new file. In this file I want to add some results I have received from my code. Unfortunately I get a error message saying I can't combine str with list. So then I tried to make a string out of the list and ended up with this

0
0
...etcetc

where the left column is the so called string and the rest is the list I tried to make a string by doing like this: str(mylist). I want to be able to write to a new file both the string and this list I have.

3
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by sofia85
0

Make a string with ' '.join(thelist) (or '\t'.join(), or 'foo'.join()). The list must contain strings. There are other methods, you can write the list items one by one in the file.

Edited by Gribouillis: n/a

0

line.split(None, 1) to split it and do eval on secoond item off the result.

>>> line = "0 ['a', 'b', 'c']"
>>> st, seq = line.split(None, 1)
>>> seq = eval(seq)
>>> st, seq
('0', ['a', 'b', 'c'])
>>>

Edited by pyTony: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.