A beginning beginner's question about input, output and . . .

2QdxY4RzWzUUiLuE at potatochowder.com 2QdxY4RzWzUUiLuE at potatochowder.com
Sat Jan 16 21:17:46 EST 2021


On 2021-01-16 at 17:46:13 -0500,
DonK <don81846 at comcast.net.invalid> wrote:

> On Sat, 16 Jan 2021 14:56:37 -0600, 2QdxY4RzWzUUiLuE at potatochowder.com
> wrote:
> 
> >On 2021-01-16 at 15:42:44 -0500,
> >DonK <don81846 at comcast.net.invalid> wrote:

> >> For example, I've found a need to parse text documents quite a number
> >> of times over the years. Basic/VB is great at doing that. How's
> >> Python?
> 
> >
> >Python can do that.  Can you expand on "parse" and "text documents"?
> 
> There's nothing that I have any particular need for at the moment but
> it is something that I've done a number of times over the years. I
> think it's a common need?? 

Indeed.  :-)  In my experience, "parse" and "text documents" often mean
different things to different people.

> I've used Pascal and BASIC\VB for string parsing and BASIC\VB is much
> better. VB has all the string handling functions that you need built
> in but, for example, Pascal has some but you have to create others to
> be useful. Since I'm just beginning with Python I have no knowledge or
> criticism of how Python does this but I'm sure that it can.
> 
> Since I've retired I've written parsers for my bank records, medical
> records and other personally useful things. I would categorize them as
> trivial but useful.  i.e utility means useful
> 
> When I was working, in the 1999-2001 range, I wrote a parser in VB
> that unscrambled corrupted "batch files" for credit card processing so
> that vendors didn't have to spend hours rebuilding them by hand. Some
> of those files had over 700 credit card transactions in them. That's
> one example.

Sounds like you're parsing files composed of lines of plain text where
each line represents some kind of record (as opposed to parsing a
document containing programming code as a precursor to generating an
executable, or looking through a word processing document for something
important).

A bare minimum skeleton might look something like this:

    with open(filename) as f:
        for line in f.readlines():
            handle_one_line(f)

Python has a capable string type for handling text; see
<https://docs.python.org/3/library/string.html>.

Or look into the fileinput module for a convenient way to run through a
collection of files and/or standard input.

> I'm looking forward to learning some Python, mostly for fun, but I'm
> sure that it will also be useful.

Absolutely!  :-)

Happy Hacking!


More information about the Python-list mailing list