user12229568
user12229568

Reputation: 15

Split characters and numbers in a string with whitespace

Suppose I have this variable

row1 = 'Other Middle East 368 102 31 29 162 54 0.0 -56.0'

row2 ='United Arab Emirates 248 689 42 23 754 243 0.0 204.0'

How can I get the countries out into a list like below?

country = ['Other Middle East','United Arab Emirates']

Great thanks in advance!

Upvotes: 0

Views: 40

Answers (1)

alec
alec

Reputation: 6112

I think the simplest way is to use regex. \D will match non digit characters, and \d will match digit characters.

import re

row1 = 'Other Middle East 368 102 31 29 162 54 0.0 -56.0'
row2 ='United Arab Emirates 248 689 42 23 754 243 0.0 204.0'
row3 = 'ASEAN (South) 248 689 42 23 754 243 0.0 204.0'

print(re.findall('(\D+?) \d', row1 + row2 + row3))

Output

['Other Middle East', 'United Arab Emirates', 'ASEAN (South)']

Upvotes: 1

Related Questions