Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Saving results
#1
Hello guys, I'm new on this website so I will try to make the best subject that I can!
I'm a french student, I study mathematics but I do a little bit of IT too.
I'm coding an algorithm for a friend and I have a problem.
My question is very "theorical" so I don't think I need to post the whole code here but if someone wants to see it I can!
So the thing is I want to register informations in two lists. No problem for that but when I restart the program obviously this informations are gone.
So my question is : how can I save this informations to use them in the following usage of my program?

P.S : I have some notions in SQL I don't know if that can help!

Thank you for reading this post and have a nice day Wink
Reply
#2
(Apr-01-2020, 05:25 PM)Rokafly Wrote: So the thing is I want to register informations in two lists. No problem for that but when I restart the program obviously this informations are gone.
So my question is : how can I save this informations to use them in the following usage of my program?
There are several ways,serialization is one way that can save and recovery original object structure
So build in has Python has pickle and json.
Json is advisable to use.
So a example with two list could be saved like this.
import json

# Your lists
lst_1 = [1, 2 ,3]
lst_2 = ['a', 'b', 'c']

# Make one list for easier saving
record = []
record.append(lst_1)
record.append(lst_2)

with open("my_file.json", "w") as j:
   json.dump(record, j)

with open("my_file.json") as j_out:
   saved_date = json.load(j_out)

print(saved_date) 
Output:
[[1, 2, 3], ['a', 'b', 'c']] # it's a fully working object >>> saved_date [[1, 2, 3], ['a', 'b', 'c']] >>> saved_date[0] [1, 2, 3] >>> saved_date[1] ['a', 'b', 'c']
There are also easy to use small DB like TinyDB | dataset(my tutorial)
sqlite3 come with Python.
Reply
#3
Thank you for your answer, I used files and it worked really well. Have a nice day!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Search Results Web results Printing the number of days in a given month and year afefDXCTN 1 2,263 Aug-21-2020, 12:20 PM
Last Post: DeaD_EyE
  How to append one function1 results to function2 results SriRajesh 5 3,210 Jan-02-2020, 12:11 PM
Last Post: Killertjuh

Forum Jump:

User Panel Messages

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