Using the OSMnx package, I am attempting to write a script which involves loading a .osm file. The .osm file was downloaded from Geofabrik.
When building, I prefer using .ipynb on VS code, but I am running into a seemingly insurmountable error (which, so far, ChatGPT has not been able to help me with) when trying to run it as a .py file from command prompt. I am using the same Conda environment for both.
Why is it that the below piece of code works perfectly fine in a .ipynb file in VS code, but returns the below error when the exact same code is run in a .py file which I am trying to call from command prompt?
Code:
network = 'network_file.osm'
G = ox.graph_from_xml(network)
Error when run as .py from command prompt:
G = ox.graph_from_xml(network)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Owner\.conda\envs\ox\Lib\site-packages\osmnx\graph.py", line 559, in graph_from_xml
response_jsons = [osm_xml._overpass_json_from_file(filepath)]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Owner\.conda\envs\ox\Lib\site-packages\osmnx\osm_xml.py", line 88, in _overpass_json_from_file
root_attrs = ET.parse(f).getroot().attrib
^^^^^^^^^^^
File "C:\Users\Owner\.conda\envs\ox\Lib\xml\etree\ElementTree.py", line 1218, in parse
tree.parse(source, parser)
File "C:\Users\Owner\.conda\envs\ox\Lib\xml\etree\ElementTree.py", line 580, in parse
self._root = parser._parse_whole(source)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Owner\.conda\envs\ox\Lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8f in position 8529: character maps to <undefined>
Failed fix attempt:
ChatGPT had suggested the below, which only returned more errors..
with open(network, 'r', encoding='UTF-8', errors='ignore') as file:
G = ox.graph_from_xml(file)
**Error**
TypeError: expected str, bytes or os.PathLike object, not TextIOWrapper
Any help or guidance would be greatly appreciated!
Windows character encoding can be a bit of a pain at times. You're probably running into this issue. You can read through that thread for details, but this will be resolved by OSMnx 1.8.0 which should be released within the coming day.