[英]Python: Reading columns and rows from SQLite file into Prettytable?
[英]python: Read a table from file (created from prettytable) and mantain spaces
假设我有以下file.txt
$ cat file.txt
+-----------+------+------------+-----------------+
| City name | Area | Population | Annual Rainfall |
+-----------+------+------------+-----------------+
| Adelaide | 1295 | 1158259 | 600.5 |
| Brisbane | 5905 | 1857594 | 1146.4 |
| Darwin | 112 | 120900 | 1714.7 |
| Hobart | 1357 | 205556 | 619.5 |
| Sydney | 2058 | 4336374 | 1214.8 |
| Melbourne | 1566 | 3806092 | 646.9 |
| Perth | 5386 | 1554769 | 869.4 |
+-----------+------+------------+-----------------+
如果我尝试像这样读取文件:
with open('file.txt') as fp:
lines = fp.readlines()
然后,我尝试使用gtk2.0文本缓冲区显示每一行(类似于print):
for eachline in lines:
if idx == 0:
self.textbuffer.delete(start, end)
end = self.textbuffer.get_end_iter()
self.textbuffer.set_text(eachline)
else:
self.textbuffer.insert(end, eachline)
end = self.textbuffer.get_end_iter()
我得到以下输出:
+-----------+------+------------+-----------------+
| City name | Area | Population | Annual Rainfall |
+-----------+------+------------+-----------------+
| Adelaide | 1295 | 1158259 | 600.5 |
| Brisbane | 5905 | 1857594 | 1146.4 |
| Darwin | 112 | 120900 | 1714.7 |
| Hobart | 1357 | 205556 | 619.5 |
| Sydney | 2058 | 4336374 | 1214.8 |
| Melbourne | 1566 | 3806092 | 646.9 |
| Perth | 5386 | 1554769 | 869.4 |
+-----------+------+------------+-----------------+
任何建议如何使其看起来更漂亮(如第一张桌子)?
提前致谢!
您可能需要像下面这样格式化字符串:
Python:格式化输出字符串,在输出中与之右对齐
for align, text in zip('<^>', ['left', 'center', 'right']):
'{0:{fill}{align}16}'.format(text, fill=align, align=align)
在输出中,您可以指定with(或根据输入的长度获取)。
我本人尚未使用gtk2.0,但也许会有所帮助https://docs.python.org/3/library/string.html#formatstrings
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.