Editing File

Jeremy Jones jemejones at gmail.com
Wed Jul 12 11:23:51 EDT 2006


D wrote:
> Thanks, guys.  So overall, would it just be easier (and not too rigged)
> if any changes were made by just editing the text file?  I want to do

<snip>

> akameswaran at gmail.com wrote:

<snip>

> > Might be overkill - but pickle the  data memeber that contains the
> > information.  If you use text instead of binary pickling it should
> > still be editable by hand.  for a single line of text it may be a bit
> > much - but it's still probably quicker than opening a file, parsing
> > etc.

Look at pickle, but I'd recommend against it if you're anticipating
needing to edit the file by hand.  It's just a little on the ugly side.
 Glance at Yaml (I think it's the pyyaml project in the cheeseshop) as
well.  Here's the code needed to "parse" in a .yaml file:

config = yaml.load(open(self.config_file, "r"))

Here's the code needed to serialize it back in a pretty format:

yaml.dump(config, config_file_obj, default_flow_style=False)

And here's a piece of a .yaml file itself:

feeds:
  http://leo.am/podcasts/floss:
    name: FLOSS Weekly
    mode: dl
  http://revision3.com/diggnation/feed/high.mp3.xml:
    name: Revision3 - Diggnation w/Kevin Rose & Alex Albrecht
    mode: dl
  http://geekmuse.net/podcast/:
    name: Geek Muse
    mode: dl

http://www.itconversations.com/rss/category-rss.php?k=achange2005&e=1:
    name: Accelerating Change 2005
    mode: dl

Nice and clean.

- Jeremy M. Jones




More information about the Python-list mailing list