.format() returning the result and Â

Viewed 71

I was doing a project on the Raspberry Pi Website, and was tracking the ISS. Then, I started playing with it, making the movement live, and putting the location in the bottom left corner (Latitude and Longitude).

I created a function to convert decimal degrees to degrees (°) minutes (') and seconds (") - a more readable format:

def to_hr(d):
  d = float(d)
  mod = d%1
  min_ = int(mod*60)
  sec = int(mod*36000%60)/10
  d = int(d)
  deg = d
  return "{}° {}' {}\"".format(deg, min_,sec)

Then I used it to display the latitude and longitude using turtle.write() to put it on the screen:

text1 = "Lat: {} Lon: {}".format(to_hr(lat), to_hr(lon))
text.color((110, 250, 85))
style = ("Arial", 7, "bold")
text.clear()
text.goto(-175, -85)
text.write(text1, font=style)

text was earlier defined as turtle.Turtle().

I don't think it was the turtle.Write() function. I think it was the result that was iffy.

Expected outputs:

-7° 3' 45.1"

153° 9' 36.5"

0° 0' 0"

-34 5' 6"

Actual outputs:

-7° 3' 45.1"

153° 9' 36.5"

0° 0' 0"

-34° 5' 6"

Does anyone know why it is doing that? In return I have no mention of this 'Â' character (Unicode is U+00C2) in my return, and it shouldn't be popping up in the result. I made d an integer, deg too.

1 Answers

As someone commented your terminal uses latin-1 (or iso-8859-1) and your turtle is writing utf-8. You can encode your output, as suggested. Perhaps even make it portable by using locale.getdefaultlocale() instead of hardcoding it.

But you could also set your locale to utf-8. I'm guessing from your StackOverflow profile and the question that you have it set to fr_CA.iso88591. If you change that to fr_CA.utf8, it should fix your problem. If I recall correctly all codepoints in iso-8859-1 output the same glyphs in utf-8, so any of your other software that outputs iso-8859-1 should continue to work when you switch to utf-8... And utf-8 has emoji, so win win... Next thing, maybe you can adapt your code to track Dogecoin, I've heard it's going to the moon.

How can I change the locale? on raspberry pi

Related