[Tutor] How to only read words within brackets/ parentheses (in .txt file) using Python
Alan Gauld
alan.gauld at btinternet.com
Wed Sep 4 13:29:49 EDT 2019
On 04/09/2019 15:39, A S wrote:
> Hi there,
>
> I am not sure if this page is active.
I'm not sure what "page" you are looking at, but this is a mailing list
although it is archived on several web sites etc.
It is alive although we did have a technical hitch last week which
disrupted service for a few days.
> I understand that reading lines in .txt files would look something like
> this in Python:
>
> with open('filename','r') as fd:
> lines = fd.readlines()
>
> However, how do I run my code to only read the words in my .txt files that
> are within each balanced parenthesis?
Normally you'd store the line ina variable and then process the line
to extract what you want. That could be by using string searches/splits,
or regular expressions or it may require a more powerful parser
If the pattern you want spans lines and especially if it can be nested
then a parser might be the best solution..
> kkkkk;
>
> select xx("xE'", PUT(xx.xxxx.),"'") jdfjhf:jhfjj from xxxx_x_xx_L ;
> quit;
> /* 1.xxxxx FROM xxxx_x_Ex_x */
> proc sql; "TRUuuuth");
> hhhjhfjs as fdsjfsj:
> select * from djfkjd to jfkjs(SELECT abc AS abc1, abc_2_ AS efg,
> abc_fg, fkdkfj_vv, jjsflkl_ff, fjkdsf_jfkj
> FROM &xxx..xxx_xxx_xxE
> where (xxx(xx_ix as format 'xxxx-xx') gff &jfjfsj_jfjfj.) and
> (xxx(xx_ix as format 'xxxx-xx') lec &jgjsd_vnv.));
Its good to see the data but what is not clear is what you want that data
to produce. Can you also show us what output "words" you expect to see?
For example in the first select line the first opening paren doesn't find a
matching close until the 3rd line down. And inside that pair there is
the PUT(...) clause. How should that be handled?
What constitutes words here?
And what should happen to the text that is not in parens?
like the opening kkkk;?
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list