|
6 | 6 | # Requires Python 3.7 or newer. Tested with 3.8 and 3.9. |
7 | 7 |
|
8 | 8 | # Installation: |
9 | | -# pip3 install opencage asyncio aiohttp backoff tqdm |
| 9 | +# pip3 install --upgrade opencage asyncio aiohttp backoff tqdm |
10 | 10 |
|
11 | 11 | import sys, random, time |
12 | 12 | import csv |
@@ -88,7 +88,11 @@ async def geocode_one_address(address, address_id): |
88 | 88 | # note: you may also want to set other optional parameters like |
89 | 89 | # countrycode, language, etc |
90 | 90 | # see the full list: https://opencagedata.com/api#forward-opt |
91 | | - geocoding_results = await geocoder.geocode_async(address, no_annotations=1) |
| 91 | + try: |
| 92 | + geocoding_results = await geocoder.geocode_async(address, no_annotations=1) |
| 93 | + except Exception as e: |
| 94 | + geocoding_results = None |
| 95 | + traceback.print_exception(e, file=sys.stderr) |
92 | 96 |
|
93 | 97 | # coordinates -> address, e.g. '40.78,-73.97' => 101, West 91st Street, New York |
94 | 98 | # lon_lat = address.split(',') |
@@ -134,9 +138,12 @@ async def main(): |
134 | 138 | ## |
135 | 139 | queue = asyncio.Queue(maxsize=max_items) |
136 | 140 |
|
137 | | - csv_reader = csv.reader(open(infile, 'r')) |
| 141 | + csv_reader = csv.reader(open(infile, 'r'), strict=True, skipinitialspace=True) |
138 | 142 |
|
139 | 143 | for row in csv_reader: |
| 144 | + if len(row) == 0: |
| 145 | + raise Exception("Empty line in input file at line number %d, aborting" % csv_reader.line_num) |
| 146 | + |
140 | 147 | work_item = {'id': row[0], 'address': row[1]} |
141 | 148 | await queue.put(work_item) |
142 | 149 | if queue.full(): |
|
0 commit comments