PythonDataAccess: Difference between revisions

From CASA Guides
Jump to navigationJump to search
(Created page with "'''Back to the PythonOverview.''' # # We'll take a quick look at saving and loading files focusing on two # approaches: reading and writing text files and saving variables ...")
 
No edit summary
Line 1: Line 1:
'''Back to the [[PythonOverview]].'''
'''Back to the [[PythonOverview]].'''


#
We'll take a quick look at saving and loading files focusing on two approaches: reading and writing text files and saving variables via "pickling" (think IDL save/restore). Loosely related, we'll see how to accept input from the user.
# We'll take a quick look at saving and loading files focusing on two
# approaches: reading and writing text files and saving variables via
# "pickling" (think IDL save/restore). Loosely related, we'll see how
# to accept input from the user.
#


# ------------------------------------------------------------
==Input==
# Input
 
# ------------------------------------------------------------
Input can be accepted from the command line (or a script paused) using the raw input command.
#
# Input can be accepted from the command line (or a script paused)
# using the raw input command.


verb = raw_input("Give me a verb: ")
verb = raw_input("Give me a verb: ")
Line 21: Line 13:
print mad_lib
print mad_lib


# ------------------------------------------------------------
==File Access==
# File access
 
# ------------------------------------------------------------
Python provides easy basic file access. Grab our example_file.txt for the following example.
#
 
# Python provides easy basic file access. Grab our example_file.txt
Open a file like so:
# for the following example.
#


# Open a file like so:
a_file = open("example_file.txt", "r")
a_file = open("example_file.txt", "r")


# r - means read, w means write. You can do both at once if you
r - means read, w means write. You can do both at once if you want. Read up for more.
# want. Read up for more.


# Now that it's open we can read the lines in the file into a list
Now that it's open we can read the lines in the file into a list like so:
# like so:


lines = a_file.readlines()
lines = a_file.readlines()
print lines
print lines


# we could have read a single line with readline() or only a fixed set
We could have read a single line with readline() or only a fixed set of bytes with read()
# of bytes with read()
 
Close the file


# Close the file
a_file.close()
a_file.close()


# We can also write using similar syntax (use an extra "a" to append):
We can also write using similar syntax (use an extra "a" to append):


a_new_file = open("new_file.txt", "w")
a_new_file = open("new_file.txt", "w")
Line 56: Line 43:
os.system('cat new_file.txt')
os.system('cat new_file.txt')


# note that you need to convert to strings vefore writing.
note that you need to convert to strings vefore writing.
 
Pickling


# ------------------------------------------------------------
It's possible to directly save and load variables from a file (without making them into strings and worrying about parsing).
# Pickling
# ------------------------------------------------------------
#
# It's possible to directly save and load variables from a file
# (without making them into strings and worrying about parsing).
#


import pickle
import pickle


# Make a dictionary
Make a dictionary
 
a_dict = {"field1":100,
a_dict = {"field1":100,
           50:[1,2,3,5],
           50:[1,2,3,5],
           3.14:"hello"}
           3.14:"hello"}


# Save the dictionary
Save the dictionary
 
f = open("pickle.jar","w")
f = open("pickle.jar","w")
p = pickle.Pickler(f)
p = pickle.Pickler(f)
Line 79: Line 64:
f.close()
f.close()


# Go ahead and have a look at what it's doing.
Go ahead and have a look at what it's doing.
 
import os
import os
os.system("cat pickle.jar")
os.system("cat pickle.jar")
# ... ascii but not english.


# Get the stuff back
ascii but not english.
 
Get the stuff back
 
f = open("pickle.jar","r")
f = open("pickle.jar","r")
u = pickle.Unpickler(f)
u = pickle.Unpickler(f)
Line 93: Line 81:
print read_back
print read_back


# there's also a more compact syntax to just load and dump directly
There's also a more compact syntax to just load and dump directly from a file. Options allow binary instead of ascii writing. And there's a faster version called cPickle.
# from a file. Options allow binary instead of ascii writing. And
 
# there's a faster version called cPickle.
Pickle is stack-based by the way, so:


# Pickle is stack-based by the way, so:
a = 1
a = 1
b = 2
b = 2
c = 3
c = 3


# Save the dictionary
Save the dictionary
 
f = open("another_pickle.jar","w")
f = open("another_pickle.jar","w")
p = pickle.Pickler(f)
p = pickle.Pickler(f)
Line 110: Line 98:
f.close()
f.close()


# Get the stuff back
Get the stuff back
 
f = open("another_pickle.jar","r")
f = open("another_pickle.jar","r")
u = pickle.Unpickler(f)
u = pickle.Unpickler(f)
Line 117: Line 106:
var3 = u.load()
var3 = u.load()


# ... a variable too far:
... a variable too far:
 
var4 = u.load()
var4 = u.load()
# uhoh!
 
uhoh!


f.close()
f.close()
Line 125: Line 116:
print var1, var2, var3
print var1, var2, var3


# Of course the disadvantage of pickling is that you need to unpickle
Of course the disadvantage of pickling is that you need to unpickle it. This is not a generic format to save data and share with other people.
# it. This is not a generic format to save data and share with other
# people.


#
Other approaches:
# Other approaches:
#
You don't need to waste a lot of effort duplicating previous work on reading and writing text files. Adam Ginsburg's "readcol.py" (loosely patterned after the IDL version, linked from the page) will save you a lot of effort. The package astroasciidata also looks promising but I have not yet gotten a chance to experiment with it.
# You don't need to waste a lot of effort duplicating previous work on
# reading and writing text files. Adam Ginsburg's "readcol.py"
# (loosely patterned after the IDL version, linked from the page) will
# save you a lot of effort. The package astroasciidata also looks
# promising but I have not yet gotten a chance to experiment with it.
#

Revision as of 18:59, 1 November 2011

Back to the PythonOverview.

We'll take a quick look at saving and loading files focusing on two approaches: reading and writing text files and saving variables via "pickling" (think IDL save/restore). Loosely related, we'll see how to accept input from the user.

Input

Input can be accepted from the command line (or a script paused) using the raw input command.

verb = raw_input("Give me a verb: ") noun = raw_input("Give me a noun: ")

mad_lib = "More fun than "+verb+"ing a "+noun print mad_lib

File Access

Python provides easy basic file access. Grab our example_file.txt for the following example.

Open a file like so:

a_file = open("example_file.txt", "r")

r - means read, w means write. You can do both at once if you want. Read up for more.

Now that it's open we can read the lines in the file into a list like so:

lines = a_file.readlines() print lines

We could have read a single line with readline() or only a fixed set of bytes with read()

Close the file

a_file.close()

We can also write using similar syntax (use an extra "a" to append):

a_new_file = open("new_file.txt", "w") a_new_file.writelines(lines) a_new_file.close()

import os os.system('cat new_file.txt')

note that you need to convert to strings vefore writing.

Pickling

It's possible to directly save and load variables from a file (without making them into strings and worrying about parsing).

import pickle

Make a dictionary

a_dict = {"field1":100,

         50:[1,2,3,5],
         3.14:"hello"}

Save the dictionary

f = open("pickle.jar","w") p = pickle.Pickler(f) p.dump(a_dict) f.close()

Go ahead and have a look at what it's doing.

import os os.system("cat pickle.jar")

ascii but not english.

Get the stuff back

f = open("pickle.jar","r") u = pickle.Unpickler(f) read_back = u.load() f.close()

print a_dict print read_back

There's also a more compact syntax to just load and dump directly from a file. Options allow binary instead of ascii writing. And there's a faster version called cPickle.

Pickle is stack-based by the way, so:

a = 1 b = 2 c = 3

Save the dictionary

f = open("another_pickle.jar","w") p = pickle.Pickler(f) p.dump(a) p.dump(b) p.dump(c) f.close()

Get the stuff back

f = open("another_pickle.jar","r") u = pickle.Unpickler(f) var1 = u.load() var2 = u.load() var3 = u.load()

... a variable too far:

var4 = u.load()

uhoh!

f.close()

print var1, var2, var3

Of course the disadvantage of pickling is that you need to unpickle it. This is not a generic format to save data and share with other people.

Other approaches:

You don't need to waste a lot of effort duplicating previous work on reading and writing text files. Adam Ginsburg's "readcol.py" (loosely patterned after the IDL version, linked from the page) will save you a lot of effort. The package astroasciidata also looks promising but I have not yet gotten a chance to experiment with it.