I am retrieving a lot of data in the form
6800 MAIN ST
How can I format it so that it looks normal (one space between the number and the street name), such as:
6800 MAIN ST
I am retrieving a lot of data in the form
6800 MAIN ST
How can I format it so that it looks normal (one space between the number and the street name), such as:
6800 MAIN ST
In [733]: s='6800 MAIN ST'
In [734]: ' '.join(s.split())
Out[734]: '6800 MAIN ST'
You can also use re
as @NPE mentioned, while it's not quite fast even if you get the regex pattern compiled. Benchmark:
In [746]: s='asdf fasd zzzzzz ddddddd z'
In [747]: timeit ' '.join(s.split())
1000000 loops, best of 3: 545 ns per loop
In [748]: ptn=re.compile(r"\s+")
In [749]: timeit re.sub(ptn, ' ', s)
100000 loops, best of 3: 4.08 us per loop
One way is to use a regular expression:
In [8]: s = "6800 MAIN ST"
In [9]: re.sub(r"\s+", " ", s)
Out[9]: '6800 MAIN ST'