Link to home
Start Free TrialLog in
Avatar of asrawahi
asrawahiFlag for United Kingdom of Great Britain and Northern Ireland

asked on

How to create a python program to capture data to a file?

I was given an assignment to capture the following in the input screen:

Student Name
Student ID
Gender
Student Address

Is there any way to save these user inputs into a file and query them or update them by using python?
Avatar of Dan Craciun
Dan Craciun
Flag of Romania image

We are prohibited on EE to do someone's homework, but can give pointers.

For ex, one way to write in a file in Python is:

f = open('myfile','w')
f.write('hello world\n') # \n is a line break
f.close()

Open in new window


HTH,
Dan
Avatar of techtonik
techtonik

> Is there any way to save these user inputs into a file and query them or update them
> by using python?

Yes. Python allows to save data structures to file and restore them. The process is called serialization. See pickle module for details.
@techtonik: Actually, saving a data and saving the data structures are two slightly different things. The serialization is for the second purpose. More often, it is used for passing the parts of the data structures throught network or for storing the state of the program say during hibernation.
@pepr: I don't see how it contradicts with the question. Operating with data in structured manner is more convenient than manually converting everything to strings. There is no requirement to save user input into human readable format, so for this question saving data and saving structured data is the same.
ASKER CERTIFIED SOLUTION
Avatar of pepr
pepr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So the solution was that pickle is hard. Could not have guessed from the OP :)
Well, the question tag even says "Python list", which makes sense in the context.

@Dan: pickle is possibly a bit sour to be swallowed by beginners to get that "Aha!" kind of understanding :)