Re: How to only read words within brackets/ parentheses (in .txt file) using Python

2019-09-05 Thread pankaj . jangid
A S  writes:

> 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?
>
> I am not sure how to go about it, let's say my .txt file contents lines like 
> this:
>
> k;
>
> select xx("xE'", PUT(xx..),"'") jdfjhf:jhfjj from _x_xx_L ;
> quit; 
>
> The main idea is to read only these portions of the .txt file (i.e. Those 
> within parentheses):
>

This should work for the outer parenthesis:

import re

p = re.compile(r"\((.+)\)", re.VERBOSE)

with open('filename','r') as fd:
lines = fd.readlines()
for line in lines:
m = p.findall(line)
for s in m:
print(s)


-- 
Pankaj Jangid
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to only read words within brackets/ parentheses (in .txt file) using Python

2019-09-04 Thread Peter Otten
A S wrote:

> 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?
> 
> I am not sure how to go about it, let's say my .txt file contents lines
> like this:
> 
> k;
> 
> select xx("xE'", PUT(xx..),"'") jdfjhf:jhfjj from _x_xx_L ;
> quit;



> The main idea is to read only these portions of the .txt file (i.e. Those
> within parentheses):
> 
>  ("xE'", PUT(xx..),"'") jdfjhf:jhfjj from _x_xx_L ;
> quit;

But jdfjh... is not within parens...
and what about quoted parens "("? Do they count?

You probably need a tokenizer for the SQL dialect used in your "text" file.
But first: can you give a non-technical description of what problem you are 
trying to solve instead of how you want to solve it?

Perhaps someone here knows of a better approach than counting parens.

-- 
https://mail.python.org/mailman/listinfo/python-list


How to only read words within brackets/ parentheses (in .txt file) using Python

2019-09-04 Thread A S
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?

I am not sure how to go about it, let's say my .txt file contents lines like 
this:

k;

select xx("xE'", PUT(xx..),"'") jdfjhf:jhfjj from _x_xx_L ;
quit; 

/* 1.x FROM _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_xxE
   where (xxx(xx_ix as format '-xx') gff _jfjfj.) and 
  (xxx(xx_ix as format '-xx') lec _vnv.)
);


The main idea is to read only these portions of the .txt file (i.e. Those 
within parentheses):

 ("xE'", PUT(xx..),"'") jdfjhf:jhfjj from _x_xx_L ;
quit; 

/* 1.x FROM _x_Ex_x */ 
proc sql; "TRUuuuth")

(SELECT abc AS abc1, abc_2_ AS efg, abc_fg, fkdkfj_vv, jjsflkl_ff, fjkdsf_jfkj
FROM _xxx_xxE
   where (xxx(xx_ix as format '-xx') gff _jfjfj.) and 
  (xxx(xx_ix as format '-xx') lec _vnv.)
)



Any help will be truly appreciated
-- 
https://mail.python.org/mailman/listinfo/python-list