duckman duckman - 3 months ago 8
Python Question

find text that match certain string using regex

I would like to find whether the string contains a certain character(s). if it does then output into a file, if it doesn't then output into another file.
My input data looks like this:

exchange security volume
TO AAA 193099
TO AAB 81000
TO AAH 2310
TO AAV 1161144
TO AAVdbh 675000
TO ABC 98050


So far I have tried this:

for row in data:
if 'a' in row['security'] then .....


However, I would like to use regex to match the string i.e. if string contains any lower character then ignore.
Thank you so much!

Answer

You may use filter with lambda function if only upper values are required.

>>> x = 'PrinOnlyUpperCaseLetter'
>>> filter(lambda x: x.isupper(), x)
'POUCL'

If you want both but in different list:

>>> x = 'PrinOnlyUpperCaseLetter'
>>> upper_list, lower_list = [], []
>>> for i in x:
...     if i.isupper(): 
...         upper_list.append(i)
...     else:
...         lower_list.append(i)