0
votes

I have list which is saving in file while while returning I need to (https://docs.python.org/3.8/library/ast.html#ast.literal_eval ) do ast.literal_eval method to getting as element

my list is having combination lst = FirstName, empid,age,salary,filename

example my list which is saved in file is ['Joe',101,31,99292,'/home/Joe/Joe.txt'], If I need to pass this to return I need to use ast.literal_eval

How to save with the list in to file with help of pickle and how to return it?

1

1 Answers

0
votes

Here's a way to save pickle into a file, and then load it again:

Save:

s = ['Joe',101,31,99292,'/home/Joe/Joe.txt']

with open("my_file.pcl", "wb") as f:
    p = pickle.dumps(s)
    f.write(p)

Load:

with open("my_file.pcl", "rb") as f:
    p = f.read()
    s = pickle.loads(p)
    print(s)

Result:

['Joe', 101, 31, 99292, '/home/Joe/Joe.txt']