Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Project
#1
My son is studying Python at school. My knowledge is very basic in Python, but I do understand programming languages and coding. Unfortunately I an unable to help in with this problem he is trying to solve in his project and would appreciate any help or pointers for him.
He has got to a stage in his project that he needs to save data to a file and then later extract that data to use.
Being a teenager it has been quiet awkward to get anything from him. It started as he needs help with 'Files'.
Eventually I got the example that he needs to save peoples ages to a file and then at a later stage use or extract that data.

I think he needs to save data like

Andrew 40
Sarah 33
Harry 30
Mary 48

He then needs to extract the data to be able to print it.

Remember this is a school project so would be simple and not several pages of complex code. Hopefully this makes sense or his term 'files'
Reply
#2
Welcome to the forum, sprocket.
Reading and writing files is not difficult in Python. Please read all about it in Reading and Writing Files.
In the simplest form you can do this:
myfile = open("myfile.txt", "w")
myfile.write("Andrew 40\n")
myfile.close()
Perhaps ending the lines with "\n" is a little bit technical. It is translated to a "newline" character. In fact this makes a normal text file in Unix/Linux. In Windows one should perhaps better use: "\r\n". But to overcome these technical details one could also use the "print()" function. This function is aware of the environment it works in.
myfile = open("myfile.txt", "w")
print("Andrew 40", file=myfile)
myfile.close()
I hope this gets you on the right track.
Reply
#3
Lots of different ways to work with saved data in Python. Simplest is probably the text file as above. A little fancier but still pretty simple is a csv file. Then json, and then databases. Even databases in Python don't end up being pages of code. All depends on how much structure he wants to place on his data.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020