PythonDataAccess

From CASA Guides
Revision as of 22:55, 1 November 2011 by Aleroy (talk | contribs) (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 ...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

Back to the PythonOverview.

  1. We'll take a quick look at saving and loading files focusing on two
  2. approaches: reading and writing text files and saving variables via
  3. "pickling" (think IDL save/restore). Loosely related, we'll see how
  4. to accept input from the user.
  1. ------------------------------------------------------------
  2. Input
  3. ------------------------------------------------------------
  4. Input can be accepted from the command line (or a script paused)
  5. 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

  1. ------------------------------------------------------------
  2. File access
  3. ------------------------------------------------------------
  4. Python provides easy basic file access. Grab our example_file.txt
  5. for the following example.
  1. Open a file like so:

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

  1. r - means read, w means write. You can do both at once if you
  2. want. Read up for more.
  1. Now that it's open we can read the lines in the file into a list
  2. like so:

lines = a_file.readlines() print lines

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

a_file.close()

  1. 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')

  1. note that you need to convert to strings vefore writing.
  1. ------------------------------------------------------------
  2. Pickling
  3. ------------------------------------------------------------
  4. It's possible to directly save and load variables from a file
  5. (without making them into strings and worrying about parsing).

import pickle

  1. Make a dictionary

a_dict = {"field1":100,

         50:[1,2,3,5],
         3.14:"hello"}
  1. Save the dictionary

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

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

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

  1. ... ascii but not english.
  1. 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

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

a = 1 b = 2 c = 3

  1. Save the dictionary

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

  1. Get the stuff back

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

  1. ... a variable too far:

var4 = u.load()

  1. uhoh!

f.close()

print var1, var2, var3

  1. Of course the disadvantage of pickling is that you need to unpickle
  2. it. This is not a generic format to save data and share with other
  3. people.
  1. Other approaches:
  2. You don't need to waste a lot of effort duplicating previous work on
  3. reading and writing text files. Adam Ginsburg's "readcol.py"
  4. (loosely patterned after the IDL version, linked from the page) will
  5. save you a lot of effort. The package astroasciidata also looks
  6. promising but I have not yet gotten a chance to experiment with it.