[Tutor] put? and files

Kent Johnson kent37 at tds.net
Fri May 2 13:23:15 CEST 2008


On Thu, May 1, 2008 at 11:42 PM, Ross Glover <ross at ross.mayfirst.org> wrote:
> Thanks all for your in[put].  It did take me a minute to figure out that 2
> variables could get assigned using this method.  Thanks to your help, I
> managed to cobble together a fully functional GUI based dictionary program
> (MY FIRST!).
>  That said, it would seem that I do need to understand files better.  Does
> anyone have a suggestion for a solid and detailed explanation?  Either
> online or a book?

I don't think you need to know more about files than you will get in
any introductory book. The files chapter of Beginning Python should be
fine.

>  What I want to do is write a program that parses text based on tags that I
> create, writes the text into separate files based on specified tags, i.e.
> each tag gets its own file.  At least this is my starting goal.

Text parsing varies widely. At the simple end, you can use string
methods such as split(), strip() and slicing to extract the text you
want. See the strings chapter of BP or
http://docs.python.org/lib/string-methods.html

The next level of complexity uses regular expressions to extract text.
There is a bit of a learning curve to this, but regexes are a very
powerful and useful tool that you can use outside of Python as well.
They are helpful when your parser must recognize specific patterns.
http://www.amk.ca/python/howto/regex/

For more complicated formats you will want a specialized parser. For
example Python includes a parser for XML; BeautifulSoup is a popular
third-party parser for HTML; pyparsing is a fairly easy-to-use library
for building specialized parsers. You will want a specialized parser
if your text format has many variations or if it allows nested
patterns.

If you give some examples of what you want to parse we can give more
specific advice.

Kent


More information about the Tutor mailing list