This is a Python list, no pandas
in sight.
In [26]: list3= ["France","Germany","USA"]
Look at what str
produces:
In [27]: str(list3)
Out[27]: "['France', 'Germany', 'USA']"
That is one string with brackets and quotes.
What you want is more like:
In [28]: for word in list3: print(word)
France
Germany
USA
Or writing the same to a file:
In [29]: with open('txt', 'w') as f:
...: for word in list3:
...: f.write('%s\n'%word)
...:
In [30]: cat txt
France
Germany
USA
Or with print
file
parameter:
In [31]: with open('txt', 'w') as f:
...: for word in list3:
...: print(word, file=f)
or you can join the strings newlines:
In [33]: '\n'.join(list3)
Out[33]: 'France\nGermany\nUSA'
In [34]: with open('txt', 'w') as f:
...: print('\n'.join(list3), file=f)
You could put the list in pandas DataFrame
, but then you have to turn off columns and indices when writing the csv.
numpy
also does it with np.savetxt('txt',list3, fmt='%s')
.
Lots of ways of writing such a basic list of strings to a file. Some basic, some using more powerful writers.