I'm using python 3.5.2 and I'm trying to automatically open a url with parameters (many of them read from a csv-file). My problem is that one of the paramters contain the Norwegian letter "ø" in "Møre 2013" (see ...projects:"Møre%202013", where %20 is used to include a space between Møre and 2013) which causes an error message.
A bat-file runs lesurl.py with input-parameters from a csv-file. My code in the lesurl.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys # for å kunne lese variable
import urllib.request # for å lese url
fn=sys.argv[1]
minx = sys.argv[2]
miny = sys.argv[3]
maxx = sys.argv[4]
maxy = sys.argv[5]
pnavn = sys.argv[6]
c = sys.argv[7]
u = sys.argv[8]
pwd = sys.argv[9]
f = sys.argv[10]
bestilling = urllib.request.urlopen('https://tjenester.norgeibilder.no/REST/StartExport.ashx?request={username:"'+u+'",password:"'+pwd+'",copyEmail:"",comment:"'+fn+'",coordInput:{type:"Polygon",coordinates:[[['+minx+','+maxy+'],['+maxx+','+maxy+'],['+maxx+','+miny+'],['+minx+','+miny+'],['+minx+','+maxy+']]},inputWkid:'+c+',cutNationalBorder:0,format:'+f+',resolution:'+r+',outputWkid:'+c+',fillColor:255,projects:"Møre%202013",imagemosaic:2}').read()
print(bestilling)
"Møre%202013" seems to cause an error:
Traceback (most recent call last):
File "b_of_lesurl.py", line 28, in <module>
bestilling = urllib.request.urlopen('https://tjenester.norgeibilder.no/REST/StartExport.ashx?request={username:"'+u+'",password:"'+pwd+'
",copyEmail:"",comment:"'+fn+'",coordInput:{type:"Polygon",coordinates:[[['+minx+','+maxy+'],['+maxx+','+maxy+'],['+maxx+','+miny+'],['+minx
+','+miny+'],['+minx+','+maxy+']]},inputWkid:'+c+',cutNationalBorder:0,format:'+f+',resolution:'+r+',outputWkid:'+c+',fillColor:255,projects
:"Møre%202013",imagemosaic:2}').read()
File "C:\Users\ban\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 163, in urlopen
return opener.open(url, data, timeout)
File "C:\Users\ban\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 466, in open
response = self._open(req, data)
File "C:\Users\ban\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 484, in _open
'_open', req)
File "C:\Users\ban\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 444, in _call_chain
result = func(*args)
File "C:\Users\ban\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 1297, in https_open
context=self._context, check_hostname=self._check_hostname)
File "C:\Users\ban\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 1254, in do_open
h.request(req.get_method(), req.selector, req.data, headers)
File "C:\Users\ban\AppData\Local\Programs\Python\Python35-32\lib\http\client.py", line 1106, in request
self._send_request(method, url, body, headers)
File "C:\Users\ban\AppData\Local\Programs\Python\Python35-32\lib\http\client.py", line 1141, in _send_request
self.putrequest(method, url, **skips)
File "C:\Users\ban\AppData\Local\Programs\Python\Python35-32\lib\http\client.py", line 983, in putrequest
self._output(request.encode('ascii'))
UnicodeEncodeError: 'ascii' codec can't encode character '\xf8' in position 344: ordinal not in range(128)
I have tried different variants of encode('utf-8') like (and using ...projects:"'+s+'",...
in urlopen.
s="Møre%202013"
print(s)
s=s.encode('utf-8')
print(s)
giving
Møre%202013
b'M\xc3\xb8re%202013'
b'M\xc3\xb8re%202013'
and still giving the encode error. How do I include "ø" correctly? (Btw, e.g. "Oslo 2015" works fine.)