输出列表的格式

时间:2017-10-24 09:47:10

标签: python os.walk output-formatting

在python中有一段代码:

print (sorted([(r,len(f)) for r,d,f in os.walk(directory_path)],key=lambda x : x[1],reverse = True))

,输出如下:

[('/etc/ssl/certs', 595), ('/etc/alternatives', 109), ('/etc/', 73), ('/etc/init.d', 52),('/etc/dovecot/conf.d', 27)]

我想要类似的东西(不将其存储到变量中):

('/etc/ssl/certs', 595)
('/etc/alternatives', 109)
('/etc/', 73)
('/etc/init.d', 52)
('/etc/dovecot/conf.d', 27)]

2 个答案:

答案 0 :(得分:1)

使用pprint

import pprint
pprint.pprint(sorted([(r,len(f)) for r,d,f in os.walk(directory_path)],key=lambda x : x[1],reverse = True))

pprint.pprint([('/etc/ssl/certs', 595), ('/etc/alternatives', 109), ('/etc/', 73), ('/etc/init.d', 52),('/etc/dovecot/conf.d', 27)])
# [('/etc/ssl/certs', 595),
#  ('/etc/alternatives', 109),
#  ('/etc/', 73),
#  ('/etc/init.d', 52),
#  ('/etc/dovecot/conf.d', 27)]

答案 1 :(得分:-1)

你有没有试过这样的东西?

dbSearch_method: function () {
        var self = this;
        $.ajax({
            url: 'Home/LocalSearch',
            type: 'GET',
            success: function (response) {
                self.searchResultObject = response;
                this.$emit('customEvent', response);
            },
            error: function () {
                alert('error');
            }
        });
    }