The current snippet works with text files (e.g. CSV):
import urllib.request
url = 'http://www.nemweb.com.au/mms.GRAPHS/GRAPHS/GRAPH_5QLD1.csv'
data = urllib.request.urlopen(url).read().decode('utf-8')
But fails for other formats (e.g. XLSX, XLS), since it tries to interpret the binary data as UTF-8 encoded text and fails. That is, the above code produces an error.
This starts getting into the slippery slope of having different snippets for different types of files. Not to mention, what can a user do with an Excel file they have downloaded?
Perhaps an easy interim solution is to remove the .decode('utf-8') and let the user figure out what they need to do with the binary data they have downloaded. For example, if it is CSV they will have to realise they need to convert the Python binary string into a normal string. Not a great solution, but at least the code will download something and not immediately fail on them.
The current snippet works with text files (e.g. CSV):
But fails for other formats (e.g. XLSX, XLS), since it tries to interpret the binary data as UTF-8 encoded text and fails. That is, the above code produces an error.
This starts getting into the slippery slope of having different snippets for different types of files. Not to mention, what can a user do with an Excel file they have downloaded?
Perhaps an easy interim solution is to remove the
.decode('utf-8')
and let the user figure out what they need to do with the binary data they have downloaded. For example, if it is CSV they will have to realise they need to convert the Python binary string into a normal string. Not a great solution, but at least the code will download something and not immediately fail on them.