groveling over a file for Q:: and A:: stmts

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Jul 24 04:21:08 EDT 2012


On Tue, 24 Jul 2012 00:50:22 -0700, paul618 wrote:

> #!/usr/bin/env python
> # grep_for_QA.py  I am only looking to isolate uniq Q:: and A:: stmts
> from my daily files #
> # note:  This algorithm will fail if there are any blank lines within
> the Q and A area of interest (a paragraph)
> 
> # D. Beazley is my fav documentation


If you are going to ask a question, please ask a question. Don't just 
dump a whole pile of code in our laps and expect us to work out what your 
question is.

It may help if you read this page:

http://sscce.org/

Some further comments below:

> import re, glob
> import pprint as pp
> 
> sampledata = '''
> A:: And Straight Street is playin on the Radio Free Tibet.  What are the
> chances, DTMB? Q:: About 1 in 518400, Professor.
> A:: Correct!  Err, I thought it was 1:410400, but <i>close enough for
> jazz!</i>
> 
> 
> '''
> 
> pattern0 = re.compile("Q::")

There is no point in using a regular expression for something as trivial 
as that. That is like swinging a 20 kg sledge-hammer to crack a peanut.

Just use a string method:

if my_string.startswith("Q::"): ...


[...]
>             # Later, I also need to treat Unicode -- and I am clueless.

If you have a question about Unicode, you should ask it.

If you have not already read this page, you should read it now:

http://www.joelonsoftware.com/printerFriendly/articles/Unicode.html



>         except Exception as e:
>             print("--- " + e + " ---")

Please don't throw away useful debugging information.

You should learn to read exception tracebacks, not hide them. They 
contain a lot of very useful information to help you debug your code.

> except UnicodeDecodeError:
>     #encoded_line = encoded_line.urlsafe_b64encode(re.replace("asdf",
>     encoded_line)) #line = re.sub(".+", "--- asdf ---", line) pass

This will never be caught because any UnicodeDecodeError will already be 
caught by the "except Exception" line above.


> L.sort
> print (L)
> 
> # and what"s wrong with some of this, here! #myHash = set(L)    #
> uniqify
> #pp.pprint(myHash)  # july 23, 131001 hike!

I don't know what's wrong with it. What do you expect it to do, and what 
does it actually do instead?



-- 
Steven



More information about the Python-list mailing list