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

asked Sep 6, 2012 at 17:27

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('-'))
answered Sep 6, 2012 at 17:30
Sign up to request clarification or add additional context in comments.

Comments

1
'179-53-32'.replace('-', '°', 1).replace('-', "'", 1)+ '"'
answered Sep 6, 2012 at 17:30

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'))
g.d.d.c
48.3k12 gold badges105 silver badges116 bronze badges
answered Sep 6, 2012 at 20:00

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.