Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

easy save/load of data in python

Tags:

python

io

What is the easiest way to save and load data in python, preferably in a human-readable output format?

The data I am saving/loading consists of two vectors of floats. Ideally, these vectors would be named in the file (e.g. X and Y).

My current save() and load() functions use file.readline(), file.write() and string-to-float conversion. There must be something better.

like image 352
nos Avatar asked Dec 15 '10 13:12

nos


2 Answers

The most simple way to get a human-readable output is by using a serialisation format such a JSON. Python contains a json library you can use to serialise data to and from a string. Like pickle, you can use this with an IO object to write it to a file.

import json

file = open('/usr/data/application/json-dump.json', 'w+')
data = { "x": 12153535.232321, "y": 35234531.232322 }

json.dump(data, file)

If you want to get a simple string back instead of dumping it to a file, you can use json.dumps() instead:

import json
print json.dumps({ "x": 12153535.232321, "y": 35234531.232322 })

Reading back from a file is just as easy:

import json

file = open('/usr/data/application/json-dump.json', 'r')
print json.load(file)

The json library is full-featured, so I'd recommend checking out the documentation to see what sorts of things you can do with it.

like image 187
Jamie Rumbelow Avatar answered Oct 09 '22 19:10

Jamie Rumbelow


  • If it should be human-readable, I'd also go with JSON. Unless you need to exchange it with enterprise-type people, they like XML better. :-)

  • If it should be human editable and isn't too complex, I'd probably go with some sort of INI-like format, like for example configparser.

  • If it is complex, and doesn't need to be exchanged, I'd go with just pickling the data, unless it's very complex, in which case I'd use ZODB.

  • If it's a LOT of data, and needs to be exchanged, I'd use SQL.

That pretty much covers it, I think.

like image 36
Lennart Regebro Avatar answered Oct 09 '22 20:10

Lennart Regebro