[Tutor] Writing a list in a text file
Alan Gauld
alan.gauld at btinternet.com
Sun Dec 3 10:12:10 CET 2006
"Mihai Iacob" <aymchaos at yahoo.com> wrote in
>
> def saveBugs(data):
> store = open(filename, 'w')
> for timeMark,aList in data.items():
> store.write(timeMark + '\n')
> store.write(aList + '\n')
> store.close()
>
> After i run this part the following error pops up:
>
> Traceback (most recent call last):
> store.write(aList + '\n')
> TypeError: can only concatenate list (not "str") to
> list
As the error says you can't join lists and strings,
you need to convert the list to a string first. But it
might be worth considering whether you will ever
want to read that list back into your program,
because if you do the simple str() functions
representation of your list might not be the best.
In that case you might want to write a functiuon
to save the list item by item, and another to load
the list back again.
But if you just want to store the values to read in
an editor the str() function will probably be just fine.
HTH,
--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld
More information about the Tutor
mailing list