Python 无法转换为字符串

Python 无法转换为字符串,python,python-3.x,Python,Python 3.x,我可能使用了错误的python术语。 我有一个由3个整数元素组成的数组:月、日和年。 但是,在连接字符串时,我无法打印每个单独的元素 import ssl import OpenSSL import time import sys def get_SSL_Expiry_Date(host, port): cert = ssl.get_server_certificate((host, 443)) x509 = OpenSSL.crypto.load_certificate(Op

我可能使用了错误的python术语。
我有一个由3个整数元素组成的数组:月、日和年。 但是,在连接字符串时,我无法打印每个单独的元素

import ssl
import OpenSSL
import time
import sys

def get_SSL_Expiry_Date(host, port):
    cert = ssl.get_server_certificate((host, 443))
    x509 = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, cert)
    raw_date = x509.get_notAfter()
    decoded_date = raw_date.decode("utf-8")
    dexpires = time.strptime(decoded_date, "%Y%m%d%H%M%Sz")
    bes = dexpires.tm_mon,dexpires.tm_mday,dexpires.tm_year
    print (bes)
    #print(bes[0]+"/"+bes[1]+"/"+bes[2])

domain = sys.argv[1]
port = 443
get_SSL_Expiry_Date(domain, port)
如果我取消注释第14行,我会得到一个错误:

TypeError:不支持+:'int'和'str'的操作数类型

我正在尝试以这种格式获取日期(所有字符串):
Month/date/Year


我做错了什么?

首先,您必须将int值转换为string,而不是只能将其凹化。 您可以使用
str()
内置方法

print(str(bes[0])+"/"+ str(bes[1])+"/"+ str(bes[2]))  #convert int to str first.

您可以使用Python的
format()
方法来处理它(也更简洁):

…或进一步简化(感谢安东)

只需使用:

print(time.strftime("%m/%d/%y",dexpires))
另见

一般来说,python模块通常包含所有类型的重新格式化函数,您不必重新设计它们

例如:

>>> dexpires=time.strptime('20180823131455z','%Y%m%d%H%M%Sz')
>>> dexpires
time.struct_time(tm_year=2018, tm_mon=8, tm_mday=23, tm_hour=13, tm_min=14, tm_sec=55, tm_wday=3, tm_yday=235, tm_isdst=-1)
>>> time.strftime('%m/%d/%y',dexpires)
'08/23/18'
>>>

我在尝试此解决方案时遇到此错误:AttributeError:“time.struct_time”对象没有属性“strftime”@Bes抱歉,是的,您必须调用模块函数,而不是特定时间对象的函数。更改了语法。
print(time.strftime("%m/%d/%y",dexpires))
>>> dexpires=time.strptime('20180823131455z','%Y%m%d%H%M%Sz')
>>> dexpires
time.struct_time(tm_year=2018, tm_mon=8, tm_mday=23, tm_hour=13, tm_min=14, tm_sec=55, tm_wday=3, tm_yday=235, tm_isdst=-1)
>>> time.strftime('%m/%d/%y',dexpires)
'08/23/18'
>>>