返回响应多行

时间:2019-04-12 09:21:11

标签: python python-3.x return

import sys
import logging
import rds_config
import pymysql
#rds settings
rds_host  = "xxxxxx"
name = rds_config.db_username
password = rds_config.db_password
db_name = rds_config.db_name

logger = logging.getLogger()
logger.setLevel(logging.INFO)

try:
    conn = pymysql.connect(rds_host, user=name, passwd=password, db=db_name, connect_timeout=5)
except:
    logger.error("ERROR: Unexpected error: Could not connect to MySQL instance.")
    sys.exit()

logger.info("SUCCESS: Connection to RDS MySQL instance succeeded")
def handler(event, context):
    """
    This function fetches content from MySQL RDS instance
    """

    data = ''

    with conn.cursor() as cur:

        cur.execute("Select * from xxxxx where Status = 'Active';")

        for row in cur:
            logger.info(row)

            data+= " ".join(map(str, row)) + "\n"


        return data

我收到如下响应

Response:
"1 xxxxxxx Full Active 2019-12-31\n2 yyyyyyyy Full Active 2019-12-31\n"

我想要它如下(可以不加引号吗?)

Response:
"1 xxxxxxx Full Active 2019-12-31"
"2 yyyyyyyy Full Active 2019-12-31"

Python新手。谁能帮忙吗?

1 个答案:

答案 0 :(得分:2)

"1 xxxxxxx Full Active 2019-12-31\n2 yyyyyyyy Full Active 2019-12-31\n"

是字符串的Python表示形式

1 xxxxxxx Full Active 2019-12-31
2 yyyyyyyy Full Active 2019-12-31

无需执行任何操作:您已经有多行字符串;尝试print(data)来查看。