我正在使用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}
该.fetchone()
方法返回一个序列;在这种情况下,一个元组。在这种情况下为1元组。因此,您可以通过执行以下操作来获取该1元组的第一个也是唯一的一项:
data = cursor.fetchone()[0]
那是快速而简单的。做工精美。非常感谢。在此之前,我曾尝试过其他代码,但发现它是一个元组,但仍不确定如何修复它。谢谢。我对您的答案进行了投票,但是由于我是新用户,因此并未计算在内。
不客气 您真的很容易解决,因为您在问题中做了很多工作。您可以选择带有复选标记的答案。这样人们知道问题已经解决。
谢谢。我现在按下了对勾。我什至不记得更早看到它。