无法转换为字符串

时间:2018-11-09 06:55:38

标签: 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(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: unsupported operand type(s) for +: 'int' and 'str'

我正在尝试以以下格式(所有字符串)获取日期:Month/Date/Year
我在做什么错了?

3 个答案:

答案 0 :(得分:1)

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

print("{0}/{1}/{2}".format(bes[0],bes[1],bes[2]))

...或进一步简化(感谢Anton)

print("{0}/{1}/{2}".format(*bes))

Python String Formatting

答案 1 :(得分:0)

首先,您必须将int值转换为字符串,而不仅仅是使它们凹入。    您可以使用str()内置方法

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

答案 2 :(得分:0)

只需使用:

<html>
<head>
    <title>Welcome to WE</title>
    <style type="text/css">
        .sidenav {
            height: 100%;
            width: 0;
            position: fixed;
            z-index: 1;
            top: 0;
            left: 0;
            background-color: #e8fbfc;
            overflow: hidden;
            transition: 0.5s;
            padding-top: 60px;
        }

        .sidenav a {
            padding: 8px 8px 8px 32px;
            text-decoration: none;
            font-size: 18px;
            color: #818181;
            display: block;
            transition: 0.3s;
        }

        .sidenav a:hover {
            color: black;
        }

        .sidenav .closebtn {
            position: absolute;
            top: 0;
            right: 25px;
            font-size: 36px;
            margin-left: 50px;
        }

        @media screen and (max-height: 450px) {
          .sidenav {padding-top: 15px;}
          .sidenav a {font-size: 18px;}
        }
    </style>
</head>
<body>

    <div id="mySidenav" class="sidenav">
      <a href="javascript:void(0)" class="closebtn" onclick="closeNews()">&times;</a>
        <div class="row" style="height: 600px;border: 2px solid #636e72;background:#dfe6e9; margin-left: -30px;">
            <?php if ($uid!=$sid) { ?>
                <marquee direction = "up" height="100%" onmouseover="this.stop();" onmouseout="this.start();">
                <?php
                    $uid=$_SESSION['id'];
                    $post=mysqli_query($conn,"SELECT * FROM `posts` WHERE `user_id`='$sid' AND `type`='Follower' ORDER BY `date` DESC");
                    while ($r_post=mysqli_fetch_assoc($post)) {
                        $userid=$r_post['user_id'];
                        $follower=mysqli_query($conn,"SELECT * FROM `register` WHERE `id`='$userid'");
                        $u_row=mysqli_fetch_array($follower);
                        $followerlst=mysqli_query($conn,"SELECT * FROM `follower` WHERE `sender`='$uid'");
                        while ($f_row=mysqli_fetch_assoc($followerlst)) {
                            if ($r_post['user_id']==$f_row['reciver']) {
                                ?>
                                <div class="postsingle1">
                                    <a href="activity-post.php?id=<?php echo $r_post['post_id']; ?>">   
                                        <div>
                                            <?php 
                                                $extn=substr($r_post['image'],-3);
                                                if ($extn=="mp4") {
                                                    echo "<video  width='100%' height='160px' controls><source src='uploads/".$r_post['image']."' type='video/mp4' ></video>";
                                                }
                                                else { 
                                                    echo "<img src='uploads/".$r_post['image']."' width='100%' height='160px'  />";
                                                }
                                            ?>
                                        </div>
                                    </a>
                                </div>
                                <?php
                            }
                        }
                    }
                }
                else{ ?>
                    <marquee direction = "up" height="100%" onmouseover="this.stop();" onmouseout="this.start();">
                <?php
                    $uid=$_SESSION['id'];
                    $post=mysqli_query($conn,"SELECT * FROM `posts` WHERE `user_id`='$sid' AND `type`='Profession' ORDER BY `date` DESC");
                    while ($r_post=mysqli_fetch_assoc($post)) {
                        $userid=$r_post['user_id'];
                        $follower=mysqli_query($conn,"SELECT * FROM `register` WHERE `id`='$userid'");
                        $u_row=mysqli_fetch_array($follower);
                        $followerlst=mysqli_query($conn,"SELECT * FROM `follower` WHERE `sender`='$uid'");
                        while ($f_row=mysqli_fetch_assoc($followerlst)) {
                            if ($r_post['user_id']==$f_row['reciver']) {
                                ?>
                                <div class="postsingle1">
                                    <a href="activity-post.php?id=<?php echo $r_post['post_id']; ?>">   
                                        <div>
                                            <?php 
                                                $extn=substr($r_post['image'],-3);
                                                if ($extn=="mp4") {
                                                    echo "<video  width='100%' height='160px' controls><source src='uploads/".$r_post['image']."' type='video/mp4' ></video>";
                                                }
                                                else { 
                                                    echo "<img src='uploads/".$r_post['image']."' width='100%' height='160px'  />";
                                                }
                                            ?>
                                        </div>
                                    </a>
                                </div>

                                <?php
                            }
                        }
                    }
                }
                ?>
            </marquee>
        </div>
    </div>
    <div id="mySearch" class="sidenav">
      <a href="javascript:void(0)" class="closebtn" onclick="closeSearch()">&times;</a>
        <div class="row" style="height: 600px;border: 2px solid #636e72;background:#dfe6e9; margin-left: -30px;">
            <?php if ($uid!=$sid) { ?>
                <marquee direction = "up" height="100%" onmouseover="this.stop();" onmouseout="this.start();">
                <?php
                    $uid=$_SESSION['id'];
                    $post=mysqli_query($conn,"SELECT * FROM `posts` WHERE `user_id`='$sid' AND `type`='Follower' ORDER BY `date` DESC");
                    while ($r_post=mysqli_fetch_assoc($post)) {
                        $userid=$r_post['user_id'];
                        $follower=mysqli_query($conn,"SELECT * FROM `register` WHERE `id`='$userid'");
                        $u_row=mysqli_fetch_array($follower);
                        $followerlst=mysqli_query($conn,"SELECT * FROM `follower` WHERE `sender`='$uid'");
                        while ($f_row=mysqli_fetch_assoc($followerlst)) {
                            if ($r_post['user_id']==$f_row['reciver']) {
                                ?>
                                <div class="postsingle1">
                                    <a href="activity-post.php?id=<?php echo $r_post['post_id']; ?>">   
                                        <div>
                                            <?php 
                                                $extn=substr($r_post['image'],-3);
                                                if ($extn=="mp4") {
                                                    echo "<video  width='100%' height='160px' controls><source src='uploads/".$r_post['image']."' type='video/mp4' ></video>";
                                                }
                                                else { 
                                                    echo "<img src='uploads/".$r_post['image']."' width='100%' height='160px'  />";
                                                }
                                            ?>
                                        </div>
                                    </a>
                                </div>
                                <?php
                            }
                        }
                    }
                }
                else{ ?>
                    <marquee direction = "up" height="100%" onmouseover="this.stop();" onmouseout="this.start();">
                <?php
                    $uid=$_SESSION['id'];
                    $post=mysqli_query($conn,"SELECT * FROM `posts` WHERE `user_id`='$sid' AND `type`='Profession' ORDER BY `date` DESC");
                    while ($r_post=mysqli_fetch_assoc($post)) {
                        $userid=$r_post['user_id'];
                        $follower=mysqli_query($conn,"SELECT * FROM `register` WHERE `id`='$userid'");
                        $u_row=mysqli_fetch_array($follower);
                        $followerlst=mysqli_query($conn,"SELECT * FROM `follower` WHERE `sender`='$uid'");
                        while ($f_row=mysqli_fetch_assoc($followerlst)) {
                            if ($r_post['user_id']==$f_row['reciver']) {
                                ?>
                                <div class="postsingle1">
                                    <a href="activity-post.php?id=<?php echo $r_post['post_id']; ?>">   
                                        <div>
                                            <?php 
                                                $extn=substr($r_post['image'],-3);
                                                if ($extn=="mp4") {
                                                    echo "<video  width='100%' height='160px' controls><source src='uploads/".$r_post['image']."' type='video/mp4' ></video>";
                                                }
                                                else { 
                                                    echo "<img src='uploads/".$r_post['image']."' width='100%' height='160px'  />";
                                                }
                                            ?>
                                        </div>
                                    </a>
                                </div>

                                <?php
                            }
                        }
                    }
                }
                ?>
            </marquee>
        </div>
    </div>
    <script>
        function openNews() {
            document.getElementById("mySidenav").style.width = "350px";
            document.getElementById("main").style.marginLeft = "250px";
        }

        function closeNews() {
            document.getElementById("mySidenav").style.width = "0";
            document.getElementById("main").style.marginLeft= "0";
        }
        function openSerach() {
            document.getElementById("mySearch").style.width = "350px";
            document.getElementById("mySearch").style.marginLeft = "250px";
        }

        function closeSearch() {
            document.getElementById("mySearch").style.width = "0";
            document.getElementById("mySearch").style.marginLeft= "0";
        }
    </script>
</body>
</html>

另请参阅https://docs.python.org/3/library/time.html

通常,python模块通常包含各种重新格式化功能,您无需重新发明它们。

示例:

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