python-3.x - 在 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}
解决方案
该.fetchone()
方法返回一个序列;在这种情况下,一个元组。在这种情况下,是一个 1 元组。因此,您可以通过执行以下操作来获取该 1 元组的第一个也是唯一一项:
data = cursor.fetchone()[0]
推荐阅读
- conda - 尽管在 conda-forge 中找到了包,但无法满足 conda 包:包 XXX 需要包 YYY,但无法安装任何提供程序
- c# - 在 .NET 应用程序中加载 Azure Secrets 的最佳方法
- python - 如何在多年的组合中基于月进行算术运算?
- terraform - 翻新:如何覆盖 Terraform 的默认 fileMatch
- php - 发布元字段正在正确更新 woocommerce 自定义字段,但产品页面中未显示相同内容
- python - 如何使用 tornado.web.StaticFileHandler 始终提供 1 个文件?
- android-studio - 为什么在avd的scrollView中没有显示图片内容但是显示了滚动条
- python-3.x - Jupyter notebook 无法连接到 ipython3 内核
- github - 我想为帖子设置一个主题
- c# - 为什么不会将对象添加到我的数据库中