I have a field in a table that contains string values of degrees minutes seconds in the following format:
179-53-32
I want to replace all the dashes and format this to a proper format of:
179° 53' 32"
Basically replacing the dashes with the correct character (degree, minute, second) and having spaces in there. I was thinking about doing a repalce by index, but I don't think it works:
s.replace('-', '° ')[0]
s.replace('-', '\' ')[1]
Is there a way to accomplish what I want to do? maybe enumerate the string first and then replace?
Thanks, Mike
3 Answers 3
This would probably do it:
"%s° %s' %s\"" % s.split('-')
You may need to wrap the call to s.split in a tuple call. Otherwise I was getting an error. So it becomes this:
"%s° %s' %s\"" % tuple(s.split('-'))
Comments
'179-53-32'.replace('-', '°', 1).replace('-', "'", 1)+ '"'
2 Comments
You may use groups in regular expressions. They are extremely useful.
m = re.match(r"(?P<first>\d+)-(?P<sec>\d+)-(?P<third>\d+)", "179-53-32")
"%s° %s' %s\"" % (m.group('first'), m.group('sec'), m.group('third'))