Skip to content

Instantly share code, notes, and snippets.

@KristofferK
Created October 31, 2017 00:05
Show Gist options
  • Save KristofferK/331dfe79d5a8f364620fae59a30d11e2 to your computer and use it in GitHub Desktop.
Save KristofferK/331dfe79d5a8f364620fae59a30d11e2 to your computer and use it in GitHub Desktop.
import pprint
class CountryCoordinateParser:
def __init__(self, path):
self.path = path
def parse(self):
countries = self.__getCountryAndCoordinates()
for country in countries:
countries[country] = self.__coordinateToTupleList(countries[country])
return countries
def __getCountryAndCoordinates(self):
countryCoordinates = {}
content = open(self.path, "r").read().split('\n\n')
for countryWithCoordinates in content:
countryWithCoordinates = countryWithCoordinates.split('\n')
countryCoordinates[countryWithCoordinates[0]] = ' '.join(countryWithCoordinates[1:])
return countryCoordinates
def __coordinateToTupleList(self, coordinatesDSV):
i = 0
coords = coordinatesDSV.split(' ')
for coord in coords:
coord = coord.split(';')
coord= (float(coord[0]), float(coord[1]))
coords[i] = coord
i += 1
return coords
countryCoordinateParser = CountryCoordinateParser("D:\dataset.txt")
result = countryCoordinateParser.parse()
pp = pprint.PrettyPrinter(4)
pp.pprint(result)
@KristofferK
Copy link
Author

dataset.txt contains the following

Angolar
-5.87;16.32 -6.62;16.57 -7.22;16.86
-8.98;18.13 -7.84;18.46

Albania
10.10;10.11 20.10;20.11 30.10;30.11
11.10;11.11 21.10;21.11 31.10;30.11

United Arab Emirates
24.24;51.57 23.29;51.75 24.02;10.11
24.79;54.69

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment