1

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

5

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('-'))
Sign up to request clarification or add additional context in comments.

Comments

1
'179-53-32'.replace('-', '°', 1).replace('-', "'", 1)+ '"'

2 Comments

Shouldn't there be a double quote at the end?
Thanks Rakesh. I tested this too. I had to add in the spaces, but this works for what I need as well!
0

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'))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.