我有一個函數從我的網站上的基本api獲取數組,並將其作爲文本吐出。在Python3中打印出數組時遇到一些問題
這是函數...
def avDates() :
import urllib.request
import json
response = urllib.request.urlopen('http://www.website.com/api.php')
content = response.read()
data = json.loads(content.decode('utf-8'))
dates = []
for i in data:
print(str(i['Month'])+": "+str(i['the_days']))
return dates
這個輸出該...
>>>
Apr: 16, 29, 30
May: 13, 27
Jun: 10, 11, 24
Jul: 08, 22, 23
Aug: 06, 20
Sep: 02, 03, 16, 17, 30
Oct: 01, 14, 15, 29
Nov: 25
Dec: 09, 10, 23, 24
>>>
所有我想要做的就是打印出以下..
These are the dates: -
Apr: 16, 29, 30
May: 13, 27
Jun: 10, 11, 24
Jul: 08, 22, 23
Aug: 06, 20
Sep: 02, 03, 16, 17, 30
Oct: 01, 14, 15, 29
Nov: 25
Dec: 09, 10, 23, 24
爲了我可以將它們放入基於文本或HTML的電子郵件腳本中。
我已經通過%s和str()和format()的許多組合,但我似乎無法得到正確的結果。
如果我這樣做...
from availableDates import avDates
printTest = avDates()
print ("These are the dates - %s" % ', '.join(map(str, printTest)))
我得到這個...
Apr: 16, 29, 30
May: 13, 27
Jun: 10, 11, 24
Jul: 08, 22, 23
Aug: 06, 20
Sep: 02, 03, 16, 17, 30
Oct: 01, 14, 15, 29
Nov: 25
Dec: 09, 10, 23, 24
These are the dates: -
我不知道這是爲什麼不工作 - 只是努力學習。
但是, int'行看起來好像是錯誤的方式? – dazzathedrummer
奇怪的是,如果我將「打印」行註釋掉,只留下導入和變量聲明 - 數組仍然會打印到shell。所以,我認爲,在上面的結果中,數組是從printTest行打印出來的,然後不會打印在打印行中。函數定義中是否有錯誤? – dazzathedrummer