温馨提示:本文翻译自stackoverflow.com,查看原文请点击:python 3.x - Unwanted characters when writing output from SQL query inside tcolorbox
jinja2 latex python-3.x

python 3.x - 在tcolorbox中写入来自SQL查询的输出时不需要的字符

发布于 2020-04-08 12:24:34

我正在使用python3,MariaDB,Jinja2和LaTeX + tcolorbox。我正在尝试在tcolour框内编写来自SQL查询的输出,但它在('',)内部显示,如以下屏幕快照所示:

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

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

testdb.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}

查看更多

提问者
caston
被浏览
60
Vorsprung durch Technik 2020-02-01 11:11

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

data = cursor.fetchone()[0]