# Extract only numbers and strings in long string

1688
4
07-11-2016 12:49 PM
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

1 Solution

Accepted Solutions
MVP Esteemed Contributor

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']
>>>```
4 Replies
MVP Esteemed Contributor

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']
>>>```
New Contributor

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

Dave

MVP Esteemed Contributor