How many different way to add a list in .txt file in python??? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How many different way to add a list in .txt file in python???

10th Sep 2019, 2:15 PM
Mohd Sabir
Mohd Sabir - avatar
5 Answers
+ 5
You can try this: f = open("hello.txt","a+") lst1 = "['Paul Brown', 'Coding for fun', '1234567890', 19.50]" f.write(lst1) f.close() f = open("hello.txt","a+") lst2 = "['Mary White', 'The long way', '123454321', 22.90]" f.write(lst2) f.close() f=open("hello.txt","r") print(f.read())
10th Sep 2019, 2:41 PM
Lothar
Lothar - avatar
+ 4
I suppose you already have a .txt file and you want to add new data to the end of the file?
10th Sep 2019, 2:30 PM
Lothar
Lothar - avatar
+ 3
hi, if you would have run the code exactly as it is shown on my post - it will work properly. This is in sololearn and also in other IDE. lst1 and lst2 are a list but inside quotes, do it‘s a string that contains a list.
10th Sep 2019, 7:52 PM
Lothar
Lothar - avatar
0
Actually I want to add a list in . txt entered by user in the program. Suppose we have to store data of the user
10th Sep 2019, 2:34 PM
Mohd Sabir
Mohd Sabir - avatar
0
Your lst1 is a list and we can't write like this (f.write(lst1) it will give an error coz inside write we can't put a list)
10th Sep 2019, 5:34 PM
Mohd Sabir
Mohd Sabir - avatar