我正在尝试使用打印功能来打印表中的每一行以打印每一行。
select_stmt = "SELECT user,password FROM account"
mycursor.execute(select_stmt)
myresult = mycursor.fetchall()
for row in myresult:
print (row)
当前它以随意的方式打印:
(None, 'A***9****')
(None, None)
('usertest', 'pwtest')
如何填充每一列,使其看起来更清晰?谢谢
答案 0 :(得分:2)
鉴于两列的字符数绝不能超过24个,您可以使用以下示例:
for user, pwd in myresult:
print('{: <20} {}'.format(user, pwd))
或者如果事先未知,我们可以首先确定第一列的最大大小:
len_user = max(map(lambda x: len(str(x[0])), myresult))
for user, pwd in myresult:
print('{} {}'.format(str(user).ljust(len_user), pwd))
对于样本数据,将得出:
>>> len_user = max(map(lambda x: len(str(x[0])), myresult))
>>> for user, pwd in myresult:
... print('{} {}'.format(str(user).ljust(len_user), pwd))
...
None A***9****
None None
usertest pwtest
您可以在格式中在两个{}
之间添加更多间距以增加元素之间的间距,例如:
>>> for user, pwd in myresult:
... print('{} {}'.format(str(user).ljust(len_user), pwd))
...
None A***9****
None None
usertest pwtest
对于多列,我们可以遵循相同的过程,并使用numpy计算列的最大值:
import numpy as np
lens = np.max([[len(str(xi)) for xi in x] for x in myresult], axis=0)
myformat = ' '.join(['{}']*len(lens))
for col in myresult:
print(myformat.format(*map(str.ljust, map(str, col), lens)))
答案 1 :(得分:0)
在一个表中,确实有特定数量的列。最终,您不会在控制台中编写整个列。您只能在控制台中写入选定的信息。
// Protractor configuration file, see link for more information
// https://github.com/angular/protractor/blob/master/lib/config.ts
const {SpecReporter} = require('jasmine-spec-reporter');
var HtmlScreenshotReporter = require('protractor-jasmine2-screenshot-reporter');
var reporter = new HtmlScreenshotReporter({
dest: '../build/test-results/e2e/screenshots',
filename: 'e2e-report.html',
reportOnlyFailedSpecs: true,
captureOnlyFailedSpecs: true,
showQuickLinks: true,
reportFailedUrl: true,
inlineImages: true
});
exports.config = {
SELENIUM_PROMISE_MANAGER: false,
allScriptsTimeout: 11000,
specs: [
'./src/**/*.e2e-spec.ts'
],
capabilities: {
'browserName': 'chrome',
chromeOptions: {
args: ["--headless", "--disable-gpu", "--window-size=1920,1080"]
}
},
directConnect: true,
baseUrl: 'http://localhost:4201/',
params: {},
framework: 'jasmine',
jasmineNodeOpts: {
showColors: true,
defaultTimeoutInterval: 30000,
print: function () {
}
},
beforeLaunch: function () {
return new Promise(function (resolve) {
process.on('uncaughtException', function () {
reporter.jasmineDone();
reporter.afterLaunch();
});
reporter.beforeLaunch(resolve);
});
},
onPrepare() {
require('ts-node').register({
project: require('path').join(__dirname, './tsconfig.e2e.json')
});
jasmine.getEnv().addReporter(reporter);
jasmine.getEnv().addReporter(new SpecReporter({spec: {displayStacktrace: true}}));
console.log("==========> Target report configuration", browser.params.products[process.env.TARGET_REPORT_SKU]);
},
afterLaunch: function (exitCode) {
return new Promise(function (resolve) {
reporter.afterLaunch(resolve.bind(this, exitCode));
});
}
};
或
for user, pwd in myresult:
print('{:>5} {:>5}'.format(user, pwd))
结帐https://docs.python.org/3.4/library/string.html#formatexamples以获得更多信息。
答案 2 :(得分:0)
@JR ibkr @Willem Van Onsem我能够结合使用Willem的方法和JR的方法填充所有列:
select_stmt = "SELECT site, user,password, email1, email2, comment, date FROM account"
mycursor.execute(select_stmt)
myresult = mycursor.fetchall()
len_site = max(map(lambda x: len(str(x[0])), myresult)) #site
len_user = max(map(lambda x: len(str(x[0])), myresult)) #user
len_pw = max(map(lambda x: len(str(x[0])), myresult)) #pw
len_em1 = max(map(lambda x: len(str(x[0])), myresult)) #email1
len_em2 = max(map(lambda x: len(str(x[0])), myresult)) #email2
#len_com = max(map(lambda x: len(str(x[0])), myresult)) #comments (This did not pad correctly. It protruded into the dates column)
len_dt = max(map(lambda x: len(str(x[0])), myresult)) #date
for site, user, pwd, em1, em2, com, dt in myresult:
print ('{} {} {} {} {} {:<65} {}'.format(str(site).ljust(len_site),
str(user).ljust(len_user), str(pwd).ljust(len_pw), str(em1).ljust(len_em1),
str(em2).ljust(len_em2), str(com), str(dt).ljust(len_dt)))
在正常情况下,JR,您是对的,我可能不想打印所有列,但我这样做只是为了学习。由于某些原因,注释列未正确填充,因此我手动将最大长度设置为65。不确定这是否是执行此操作的最有效方法,很想听听您的建议。谢谢