preangerian
preangerian

Reputation: 13

Python - \xb in strings

I am quite new in Python and here in this code, I am trying to write a code that reads a text file containing list of cities and their respective longitudes and latitudes, and then returns them as a dictionary containing the list of the cities including their longitudes and latitudes.

The text file looks like this:

Name: Koln Latitude: 4° 45' N Longitude: 2° 55' W
Name: Amersfoort Latitude: 1° 23' N Longitude: 2° 23' E

And my code is like this:

import re

def controller(filename):
    citydict = {}
    filevar = open(filename, 'r')
    for line in filevar:
        city = delegate(line)
        citydict[city[0]] = city
    filevar.close()
    return citydict

def delegate(ln):
    pattern = "Name: (.*) Latitude: (.*)? (.*)' (.) Longitude: (.*)? (.*)' (.)"
    matcher = re.compile(pattern)
    match = matcher.search(ln)
    name = match.group(1)
    latitude = match.group(2), match.group(3), match.group(4)
    longitude = match.group(5), match.group(6), match.group(7)
    city = (name, latitude, longitude)
    return city

print controller('cities.txt')

The code runs well but somehow, the got weird output like 2\xb in it. Anyone know what that means and how to fix them?

{'Koln': ('Koln', ('4\xb0', '45', 'N'), ('2\xb0', '55', 'W')), 'Amersfoort': ('Amersfoort', ('1\xb0', '23', 'N'), ('2\xb0', '23', 'E'))}

Upvotes: 1

Views: 1883

Answers (2)

Dadep
Dadep

Reputation: 2788

it correspond to the ° in unicode :

>>> print u'\xb0'
°

Upvotes: 0

Harvey
Harvey

Reputation: 5821

Your regular expression has a mistake. The ? says to match the previous expression, (.*), zero or one time(s).

(.*)?

If the degree character is always there, you can do this:

(.*).

Upvotes: 1

Related Questions