| 9 | |
| 10 | |
| 11 | def deserialize_places(src): |
| 12 | lines = src.splitlines() |
| 13 | # Skip header. |
| 14 | lines = lines[1:] |
| 15 | countries = defaultdict(list) |
| 16 | mwms = [] |
| 17 | |
| 18 | try: |
| 19 | for l in lines: |
| 20 | cells = l.split('\t') |
| 21 | |
| 22 | if len(cells) < 5 and not cells[0]: |
| 23 | logging.error("Country cell is empty. Incorrect line: {}".format(cells)) |
| 24 | exit() |
| 25 | |
| 26 | # Add full country. |
| 27 | if len(cells) < 3: |
| 28 | countries[cells[0]] = [] |
| 29 | # Add city of the country. |
| 30 | elif len(cells) < 5: |
| 31 | countries[cells[0]].append(cells[2]) |
| 32 | # Add mwm. |
| 33 | elif len(cells) >= 5: |
| 34 | mwms.append(cells[4]) |
| 35 | except IndexError as e: |
| 36 | logging.error("The structure of src file is incorrect. Exception: {}".format(e)) |
| 37 | exit() |
| 38 | |
| 39 | return countries, mwms |
| 40 | |
| 41 | |
| 42 | def convert(src_path, dst_path): |