我有一个函数从我的网站上的基本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