Python 裁剪数据库返回变量

Python clipping database returned variables

我正忙于使用脚本从数据库中获取一些数据并稍后安排输出,我想知道如何剪辑返回的变量

我有我的剧本

# CONNECT TO DATABASE
#=====================================================================
varPgSQL = connectToDatabase()

# PREPARE SQL STATEMENTS
#=====================================================================
cur_users = varPgSQL.cursor()
cur_users.execute("prepare cur_users as " +
                  "select * from users " +
                  "where usr_idno > ")

cur_details = varPgSQL.cursor()
cur_details.execute("prepare cur_details as " +
                    "select * from details " +
                    "where dtl_usr_idno = ")

# EXECUTE SQL STATEMENTS
#=====================================================================
la_results = []

cur_users.execute("execute cur_users(%s)", str(0))
for lr_user in cur_users:

    cur_details.execute("execute cur_details(%s)", str(lr_user[0]))
    for lr_detail in cur_details:

        # STORE RESULTS
        la_results.append({
            "usr_idno": lr_user[0],
            "usr_name": lr_user[1],
            "dtl_usr_idno": lr_detail[0],
            "dtl_usr_accn": lr_detail[1],
            "dtl_usr_accs": lr_detail[2]
        })

# CLOSE CONNECTION
#=====================================================================
varPgSQL.close()

# CHECK RESULTS
#=====================================================================
for lr_result in la_results:
    print(
        " | " +
        lr_result["usr_name"] +
        " | " +
        lr_result["dtl_usr_accn"] +
        " | " +
        lr_result["dtl_usr_accs"] +
        " | "
    )

这段代码的输出虽然没有剪裁变量,但输出是

| mavis           | service acc     | active     |

这是我所期望的,因为它是数据库中字段的长度,但是否可以剪切输出变量以实现

| mavis | service acc | active |

如果空格是由空格造成的,您可以使用内置的字符串方法 strip()

如果它是数据库工件,您可能需要向我们提供更多信息。