#python 3 version
...
#關於產地
...
crop = '牛蒡'
...
#要求輸入資料
def rundatainputcircle():
marketinput = input('*請擇一輸入: 1:台北一, 2:台北二, 3:三重市, 4:台中市, 5:高雄市, 6:鳳山市, 7:桃園縣 或是不填寫 > ')
if marketinput == '':
market = ''
elif len(str(marketinput)) ==1 and 1 <= int(marketinput) <= 7:
market = uriba[uribalist[int(marketinput)-1] + 1]
else:
print('請重新輸入:1:台北一, 2:台北二, 3:三重市, 4:台中市, 5:高雄市, 6:鳳山市, 7:桃園縣 或是不填寫 > ')
rundatainputcircle()
rundatainputcircle()
#匯入資料
def rundatacircle():
url = 'http://m.coa.gov.tw/OpenData/FarmTransData.aspx?' + '$top=' + top + '&$skip=0&crop=' + crop + '&StartDate=' + startdate + '&EndDate=' + enddate
if market != '':
url += '&Market=' + market
else:
url = url
url = url.encode('ascii')
print(url)#test
urllib.request.urlretrieve(url, "data.gz")
data_str = open('data.gz', 'r').read()#gzip.open('data.gz', 'r').read()
gobou_data = json.loads(data_str)
print(len(gobou_data))#test
return gobou_data
rawdata = rundatacircle()
And it shows a mistake:
UnicodeEncodeError: 'ascii' codec can't encode characters in position 55-56: ordinal not in range(128)
The part of mistake message:
------ UnicodeEncodeError Traceback (most recent call last) in () 92 print(len(gobou_data))#test 93 return gobou_data ---> 94 rawdata = rundatacircle() 95 96 #開始按照月份把資料載下來,從今天所屬的這個月,一直自動存到資料的最開頭101.01.01
in rundatacircle() 87 url = url 88 print(url)#test ---> 89 urllib.request.urlretrieve(url, "data.gz")#python 3 getting pics from url 90 data_str = open('data.gz', 'r').read()#gzip.open('data.gz', 'r').read() 91 gobou_data = json.loads(data_str)
/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py in urlretrieve(url, filename, reporthook, data) 185 url_type, path = splittype(url) 186 --> 187 with contextlib.closing(urlopen(url, data)) as fp: 188 headers = fp.info() 189
....it is very long, so I don't list the whole message.
I had tried many methods on Google and stackflow, but couldn't solve this problem. And I cannot understand what the error meaning.
p.s. A problem at this part of code. And I use python 3.5
Crop is a Chinese word, and it shouldn't change or be deleted. If it had been deleted, the data will go wrong....