首页 > 解决方案 > 在 tcolorbox 中写入 SQL 查询的输出时不需要的字符

问题描述

我正在使用 python3、MariaDB、Jinja2 和 LaTeX +tcolorbox。我正在尝试将 SQL 查询的输出写入 tcolour 框内,但它显示在 (' ',) 内,如以下屏幕截图所示:

显示 ('10.0.38-MariaDB-0ubuntu0.16.04.1',)

如何删除不需要的字符?谢谢你。请参阅下面包含的代码。

测试数据库.py

#!/usr/bin/python3
import jinja2
import os
import MySQLdb

connection = MySQLdb.connect("localhost","test","password")
cursor = connection.cursor()


# execute SQL query using execute() method.
cursor.execute("SELECT VERSION()")
# Fetch a single row using fetchone() method.
data = cursor.fetchone()
cursor.close()
connection.close()

from jinja2 import Template
latex_jinja_env = jinja2.Environment(
    block_start_string = '\BLOCK{',
    block_end_string = '}',
    variable_start_string = '\VAR{',
    variable_end_string = '}',
    comment_start_string = '\#{',
    comment_end_string = '}',
    line_statement_prefix = '%%',
    line_comment_prefix = '%#',
    trim_blocks = True,
    autoescape = False,
    loader = jinja2.FileSystemLoader(os.path.abspath('.'))
)

template = latex_jinja_env.get_template('tcsqltest.template')

print(template.render(section1="SQL query : %s " % data, data1=data, section2='SQL query shown inside tcolorbox example.'))

tcsqltest.template

\documentclass[11pt]{article}
\usepackage[many]{tcolorbox}
\begin{document}
\section{Example}
An example document using \LaTeX, Python, SQL, tcolorbox  and Jinja2.
\section{\VAR{section1}}
%# This is a sql database query output
\section{\VAR{section2}}

  \begin{tcolorbox}[space to upper,
        skin=bicolor,
        colbacklower=black!75,
        collower=white,
        title={Top Title},
        halign=center,
        valign=center,
        nobeforeafter,
        halign lower=flush right,
        bottom=0mm,
        height=3cm
    ]

        \VAR{data1}

        \tcblower
        End-Title
    \end{tcolorbox}%
\end{document}

标签: python-3.xlatexjinja2

解决方案


.fetchone()方法返回一个序列;在这种情况下,一个元组。在这种情况下,是一个 1 元组。因此,您可以通过执行以下操作来获取该 1 元组的第一个也是唯一一项:

data = cursor.fetchone()[0]

推荐阅读