在Python3中打印数组时遇到问题

在Python3中打印数组时遇到问题,python,python-3.x,Python,Python 3.x,我有一个函数,它从我网站上的一个基本api获取一个数组,并将其作为文本输出 这是函数 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

我有一个函数,它从我网站上的一个基本api获取一个数组,并将其作为文本输出

这是函数

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: -

我不知道为什么这不起作用-只是想学习。

在执行过程中,您有以下几点:

from  availableDates import avDates
printTest = avDates()
print ("These are the dates - %s" % ', '.join(map(str, printTest)))
但是在
avDates()
中,您已经逐个打印了月份:

for i in data:
    print(str(i['Month'])+": "+str(i['the_days']))
此外,在
avDates()
中,您的
dates
是一个空列表,您可以初始化它:

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
avDates
然后

从上次打印开始,您的
printTest
为空列表

为了使其正确,您应该在
日期
中放置
字符串
,而不是打印它并返回
日期

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:
        dates.append(str(i['Month'])+": "+str(i['the_days'])) #don't print it yet               
    return dates
然后在执行过程中:

from  availableDates import avDates
printTest = avDates()
print ("These are the dates - ")
for pt in printTest:
    print (pt)
那么你应该得到你所期望的:

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

在执行过程中,您有以下几点:

from  availableDates import avDates
printTest = avDates()
print ("These are the dates - %s" % ', '.join(map(str, printTest)))
但是在
avDates()
中,您已经逐个打印了月份:

for i in data:
    print(str(i['Month'])+": "+str(i['the_days']))
此外,在
avDates()
中,您的
dates
是一个空列表,您可以初始化它:

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
avDates
然后

从上次打印开始,您的
printTest
为空列表

为了使其正确,您应该在
日期
中放置
字符串
,而不是打印它并返回
日期

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:
        dates.append(str(i['Month'])+": "+str(i['the_days'])) #don't print it yet               
    return dates
然后在执行过程中:

from  availableDates import avDates
printTest = avDates()
print ("These are the dates - ")
for pt in printTest:
    print (pt)
那么你应该得到你所期望的:

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

但是,“print”行看起来好像是错误的吗?奇怪的是,如果我注释掉“print”行,只留下导入和变量声明,数组仍然会被打印到shell中。因此,我认为,在我上面的结果中,数组从printTest行打印,然后不在打印行中打印。函数定义中有什么错误吗?但是,“print”行看起来好像是错误的吗?奇怪的是,如果我注释掉“print”行,只留下导入和变量声明,数组仍然会被打印到shell中。因此,我认为,在我上面的结果中,数组从printTest行打印,然后不在打印行中打印。函数定义中有什么错误吗?这非常有效-这是一个非常有用的解释!谢谢你的帮助@(没问题!)这是一个非常有用的解释!谢谢你的帮助@(没问题!)