I need help with this python regex

Peter J. Holzer hjp-python at hjp.at
Sat Apr 28 02:55:28 EDT 2018


On 2018-04-27 21:04:49 -0700, Ed Manning wrote:
> Here is the source code.
> 
> 
> import re
> 
> 
> log = open("csg.txt", "r") # Opens a file call session.txt
> regex = re.compile(r'policy id \d+') # search for the policy ID
> regex1 = re.compile(r'log count \d+') # search for the policy ID
> 
> for match in log:
>     x = regex.findall(match)
>     y = regex1.findall(match)
> 
>     q = x + y
>     print(q)
>  
> 
> The problem I am having i when it print out ti looks like this
> 
> 
> L'Policy ID 243"|
> []
> []
> []
> []
> []
> []
> []
> []
> {'log count 777,"]
> 
> 
> 
> How so I fix the code sone that it does not print empty []

Print the result only if findall actually found something.

        hp

-- 
   _  | Peter J. Holzer    | we build much bigger, better disasters now
|_|_) |                    | because we have much more sophisticated
| |   | hjp at hjp.at         | management tools.
__/   | http://www.hjp.at/ | -- Ross Anderson <https://www.edge.org/>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20180428/1f126e30/attachment.sig>


More information about the Python-list mailing list