Extract only numbers and strings in long string

1940
4
Jump to solution
07-11-2016 12:49 PM
Dave_J_Zhang
New Contributor


Hi,

   I am trying to extract all numbers and string from a long string and store them in a array, but the my expressing always split a decimal number and make all integer numbers. Codes like following

# -*- coding: cp1252 -*-

import re

DATA = r"26°48'18.3431\"E  39°36'6.979\"N"

print re.findall(r"[\d+\w+]+", DATA)

it returns  ['26', '48', '18', '3431', 'E', '39', '36', '6', '979', 'N']

I want the statement returns ['26', '48', '18.3431', 'E', '39', '36', '6.979', 'N']

Can anyone help?

Thanks.

Dave

0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus

I am using utf-8  adjust if necessary... I will show it verbosely so you can see what I did... consolidate if needed

>>> nogood = ["°", "'", '\\','"']
>>> good = ''.join([ [i, ' '][i in nogood] for i in DATA])
>>> asList = [ i for i in good.split(" ") if i!='' ]
>>> good
'26 48 18.3431  E  39 36 6.979  N'
>>> asList
['26', '48', '18.3431', 'E', '39', '36', '6.979', 'N']
>>>

View solution in original post

4 Replies
DanPatterson_Retired
MVP Emeritus

I am using utf-8  adjust if necessary... I will show it verbosely so you can see what I did... consolidate if needed

>>> nogood = ["°", "'", '\\','"']
>>> good = ''.join([ [i, ' '][i in nogood] for i in DATA])
>>> asList = [ i for i in good.split(" ") if i!='' ]
>>> good
'26 48 18.3431  E  39 36 6.979  N'
>>> asList
['26', '48', '18.3431', 'E', '39', '36', '6.979', 'N']
>>>
Dave_J_Zhang
New Contributor

you are amazing! , Dan. That works perfectly for me. Thanks.

Dave

0 Kudos
JoeBorgione
MVP Emeritus

Hey Dave, since Dan's suggestion worked for you, how about marking the thread as answered?

That should just about do it....
0 Kudos
DanPatterson_Retired
MVP Emeritus

I guess, I will let one of the other moderators mark my answer correct then.... sigh,

0 Kudos