4

How do I save a list in a file and read it as a list type?

 2 years ago
source link: https://www.codesd.com/item/how-do-i-save-a-list-in-a-file-and-read-it-as-a-list-type.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How do I save a list in a file and read it as a list type?

advertisements

Say I have the list score=[1,2,3,4,5] and it gets changed whilst my program is running. How could I save it to a file so that next time the program is run I can access the changed list as a list type?

I have tried:

score=[1,2,3,4,5]

with open("file.txt", 'w') as f:
    for s in score:
        f.write(str(s) + '\n')

with open("file.txt", 'r') as f:
    score = [line.rstrip('\n') for line in f]

print(score)

But this results in the elements in the list being strings not integers.


You can use pickle module for that. This module have two methods,

  1. Pickling(dump): Convert Python objects into string representation.
  2. Unpickling(load): Retrieving original objects from stored string representstion.

https://docs.python.org/3.3/library/pickle.html code:

>>> import pickle
>>> l = [1,2,3,4]
>>> with open("test.txt", "wb") as fp:   #Pickling
...   pickle.dump(l, fp)
...
>>> with open("test.txt", "rb") as fp:   # Unpickling
...   b = pickle.load(fp)
...
>>> b
[1, 2, 3, 4]

Related Articles

save Btrees to a disk file and read it

I want to save a Btree(not sure a binary one) in a disk file. and then read it to the memory. some Level-order traversal may be a good way for a binary Btree. but if it is not a binary one. I build up the Btree from the leafnode to the rootnode in me

How to create a html form after downloading files and reading the json response from the php server

i am trying file uploading to php my server.file and data uploading through multi part /form-data ,the file and data received on php server but in my php server return json response .please help me how to read json response in my webpage and if its s

Java - How can I write my ArrayList to a file, and read (load) this file in the original ArrayList?

I am writing a program in Java which displays a range of afterschool clubs (E.G. Football, Hockey - entered by user). The clubs are added into the following ArrayList: private ArrayList<Club> clubs = new ArrayList<Club>(); By the followng Meth

How can I store the dictionaries in a file and read / write this file?

I am using tkinter to manage the GUI for a note retrieval program. I can pull my notes by typing a key word and hitting Enter in a text field but I would like to move my dictionary to a file so that my code space is not filled up with a massive dicti

How do I save an object in the file?

How can I save an object in the file? I have an Object obj,how can I save all its information such as: m.getName(); m.getFamily() and the others in the file???Using an ObjectOutputStream you can write the object directly into the file. ObjectOutputSt

How to compare an object list type with the type of int list

I'm trying to compare two lists with Linq or lambda expression. I don't want to use any kind of loops. if I have both of the list type of integer, I would use this expression example 1 List<int> tlistA = new List<int>(); tlistA.AddRange(new in

How do I get data from a text file and save it in a string?

How do I get data from a text file and save it into a string? For example, my text file has the numbers 1, 4, 5, 6, 8, and 10.4. These numbers can be on the same line or on separate lines. I want to concatenate them into a string, like so: 1 4 5 6 8

How to write OrderedDicts to a file and read it in a list?

I have a function which returns collections.OrderedDict() which is a payload for http post. I need to log offline data when http post fails, so I want to write all dicts into a file and read it back as a list, I know I can create a list and keep appe

How do I save an image or text file for later use?

How do I save an image, or text file for later use?(writing and creating a file)Use FileOutputStreams. Images should be written in binary mode.(DataOutput/InputStream) Search engines provide you several matches like: http://www.roseindia.net/java/exa

How do I save an object to a file?

I would like to save an object to a file, and then read it from the file easily. As a simple example, lets say I have the following 3d array: m = [[[0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0]]] Is there an easy Ruby API that I

How to use Javascript to read the local text file and read line by line?

I have a web page made by html+javascript which is demo, I want to know how to read a local csv file and read line by line so that I can extract data from the csv file.Without jQuery: document.getElementById('file').onchange = function(){ var file =

Are there any Java virtual machines that can save their state to a file and reload that state?

Are there any Java VMs which can save their state to a file and then reload that state? If so, which ones?Another option, which may or may not be relevant in your case, is to run the JVM (any JVM) inside a virtual machine. Most virtual machines offer

How do I save a list of objects in Redis using a single key-value?

I am trying to do this. I have a list of objects (Custom objects), I want to save them all in a single register in Redis, is it possible to save them as ajax somehow? I was reading about Jackson but I couldn't figure it how. So far I only have this @

How do I save a list to a .py file?

I am reading the contents of a normal text file and then trying to save it in a .py file as a list variable. Initially what I tried was reading the contents of the file and then storing them in a list. Then I wrote them to a .py file by myself. >>&g

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK