2

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!

2
  • Try if re.search(r"[a-z]", row['security']): Commented Aug 25, 2016 at 9:07
  • Or use .isupper() ... Commented Aug 25, 2016 at 9:07

2 Answers 2

1

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Pleasure is all mine :)
0

try this:

for i in row['security']:
   if re.search(r"[a-z]", i):
   #add to file 1

else: #add to file 2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.