+ 5
How to save data in lists (python) ?
I don't know how to save data. For example... I want to store user input. Any ideas?
16 Respuestas
+ 15
create list and take input from user and append to that list as much as u want
ip = [] #blank list
temp = input()
ip.append(temp) #add to list
print (ip)
+ 5
u can save it like a text file i had the same problem
file = open("newfile.txt", "w")
file.write("This has been written to a file")
file.close()
+ 3
Already answered here:
https://www.sololearn.com/Discuss/217410/how-can-save-any-input-to-a-list
Homework?
All the classroom is here, or it's same personn for both account?
+ 1
If I understood you well you wants to continue storing data in a list.
The code below will continue to ask for input and store in a list until you enter
the value True.
myList = []
tester = True
while tester:
a = input('Enter Value: ')
myList.append(a)
if a == 'True':
tester = False
print(myList)
0
I generally prefer to use the 'a' option when opening a file, as it means that the stuff that has been inputted is added to the file, rather than overwriting all the other data that is in the file already. For example, if you are going to save the results of a quiz or a game, then appending would be much more efficient. The only time I would use the 'w' option would be if I were going to update the data in a file, if there is only one set of it
0
If you want to store data as native python list object inside a file you can use standard module Pickle. Storing programming language objects inside binary files called serialization, reverse process called de-serialization. Example:
import pickle
some_list = [1,2,3]
#store data inside "some_file.p"
pickle.dump( some_list, open( "some_file.p", "wb" ) )
#load data from "some_file.p"
other_list= pickle.load( open( "save.p", "rb" ) )
0
Using pickle or json module. With json, it becomes even easier to store dictionaries data.
0
quite fine
0
you can create a While loop and store these data.
data = []
i
while True:
i = input("Enter the values of data, 'stop' for end:")
if i == "stop" :
break
data.append(i)
0
list = []
listLen = 7 #just for fun
for i in range(listLen):
print("input ")
list.append(input())
print(list)
- 1
#Do you mean something like that :
usr = input ("enter what you want to write :")
file = open("file.txt", "w")
file.write(usr)
file.close()
#If yes thank you verry much
#if i did'nt understood explain me more
- 1
you can use for loop, or while loop, and append this values
- 2
jshbshjmd
- 4
list_name = [ ]
user_input = raw_input("Tell user to enter value")
#i used raw_input() because of Python 2.7
list_name.append(user_input)
print(list_name)
- 4
inp = input()
list1=list(inp)
- 6
Use extension .phy👍