What's the Pythonic way to store a data block in a Python script? What's the Pythonic way to store a data block in a Python script? python python

What's the Pythonic way to store a data block in a Python script?


It depends on your data, but dict literals and multi-line strings are both really good ways.

state_abbr = {    'MA': 'Massachusetts',    'MI': 'Michigan',    'MS': 'Mississippi',    'MN': 'Minnesota',    'MO': 'Missouri',    }gettysburg = """Four score and seven years ago,our fathers brought forth on this continenta new nation, conceived in libertyand dedicated to the propositionthat all men are created equal."""


Use the StringIO module to create an in-source file-like object:

from StringIO import StringIOtextdata = """\Now is the winter of our discontent,Made glorious summer by this sun of York."""# in place of __DATA__ = open('richard3.txt')__DATA__ = StringIO(textdata)for d in __DATA__:    print d__DATA__.seek(0)print __DATA__.readline()

Prints:

Now is the winter of our discontent,Made glorious summer by this sun of York.Now is the winter of our discontent,

(I just called this __DATA__ to align with your original question. In practice, this would not be good Python naming style - something like datafile would be more appropriate.)


IMO it highly depends on the type of data: if you have only text and can be sure that there is not ''' or """ which micht by any chance be inside, you can use this version of storing the text. But what to do if you want, for example, store some text where it is known that ''' or """ is there or might be there? Then it is adviseable to

  • either store the data coded in any way or
  • put it in a separate file

Example: The text is

There are many '''s and """s in Python libraries.

In this case, it might be hard to do it via triple quote. So you can do

__DATA__ = """There are many '''s and \"""s in Python libraries.""";print __DATA__

But there you have to pay attention when editing or replacing the text.In this case, it might be more useful to do

$ python -c 'import sys; print sys.stdin.read().encode("base64")'There are many '''s and """s in Python libraries.<press Ctrl-D twice>

then you get

VGhlcmUgYXJlIG1hbnkgJycncyBhbmQgIiIicyBpbiBQeXRob24gbGlicmFyaWVzLg==

as output. Take this and put it into your script, such as in

__DATA__ = 'VGhlcmUgYXJlIG1hbnkgJycncyBhbmQgIiIicyBpbiBQeXRob24gbGlicmFyaWVzLg=='.decode('base64')print __DATA__

and see the result.