用Python编写格式正确的文本 [英] Writing nicely formatted text in Python
问题描述
在Python中,我正在使用以下代码编写文本文件:
f.write(filename + type + size + modified)
当然,输出看起来非常难看:
C:/Config/ControlSet/__db.006 file 56 KB 2012-Apr-30 10:00:46.467 AM
C:/Config/ControlSet dir 68881 KB 2012-Apr-30 10:00:46.396 AM
C:/Config/DataStore.avlocate file 0 KB 2012-Apr-30 09:57:42.440 AM
C:/Config/DataStoreLocate.bak file 0 KB 2012-Apr-30 09:57:42.470 AM
C:/Config/DeviceConnections/devconn.bak file 41 KB 2012-Apr-30 10:39:50.181 AM
C:/Config/DeviceConnections/devconn.cfg file 41 KB 2012-May-29 10:12:45.288 AM
但是我想对齐条目,使其看起来像这样:
C:/Config/ControlSet/__db.006 file 56 KB 2012-Apr-30 10:00:46.467 AM
C:/Config/ControlSet dir 68881 KB 2012-Apr-30 10:00:46.396 AM
C:/Config/DataStore.avlocate file 0 KB 2012-Apr-30 09:57:42.440 AM
C:/Config/DataStoreLocate.bak file 0 KB 2012-Apr-30 09:57:42.470 AM
C:/Config/DeviceConnections/devconn.bak file 41 KB 2012-Apr-30 10:39:50.181 AM
C:/Config/DeviceConnections/devconn.cfg file 41 KB 2012-May-29 10:12:45.288 AM
我的问题类似于此问题,除了我不知道文件名有多长时间会提前.我应该如何处理?
如果您可以首先获取所有文件名的列表,则可以执行以下操作:
max_width = max(len(filename) for filename in filenames)
for filename in filenames:
f.write(filename.ljust(max_width+1)+..whatever else..)
如果无法首先获取所有文件名的列表,则无法确保所有文件都对齐,因为无法知道以后是否会得到名称非常长的文件. /p>
但是,在这种情况下,我通常只假设N列通常就足够了,对于某些N,在这种情况下,您可以执行以下操作:
f.write('%-40s %6s %10s %2s\n' % (filename, type, size, modified))
In Python, I'm writing to a text file with code like:
f.write(filename + type + size + modified)
And of course the output looks really ugly:
C:/Config/ControlSet/__db.006 file 56 KB 2012-Apr-30 10:00:46.467 AM
C:/Config/ControlSet dir 68881 KB 2012-Apr-30 10:00:46.396 AM
C:/Config/DataStore.avlocate file 0 KB 2012-Apr-30 09:57:42.440 AM
C:/Config/DataStoreLocate.bak file 0 KB 2012-Apr-30 09:57:42.470 AM
C:/Config/DeviceConnections/devconn.bak file 41 KB 2012-Apr-30 10:39:50.181 AM
C:/Config/DeviceConnections/devconn.cfg file 41 KB 2012-May-29 10:12:45.288 AM
But I want to align the entries so it looks like this:
C:/Config/ControlSet/__db.006 file 56 KB 2012-Apr-30 10:00:46.467 AM
C:/Config/ControlSet dir 68881 KB 2012-Apr-30 10:00:46.396 AM
C:/Config/DataStore.avlocate file 0 KB 2012-Apr-30 09:57:42.440 AM
C:/Config/DataStoreLocate.bak file 0 KB 2012-Apr-30 09:57:42.470 AM
C:/Config/DeviceConnections/devconn.bak file 41 KB 2012-Apr-30 10:39:50.181 AM
C:/Config/DeviceConnections/devconn.cfg file 41 KB 2012-May-29 10:12:45.288 AM
My issue is similar to this question except I don't know how long the filenames will be beforehand. How should I approach this?
If you can get a list of all filenames first, then you could do something like:
max_width = max(len(filename) for filename in filenames)
for filename in filenames:
f.write(filename.ljust(max_width+1)+..whatever else..)
If you can't get a list of all filenames first, then there's no way to make sure that everything will line up, because there's no way to know if you'll later get a file whose name is really long.
In a case like this, though, I would usually just assume that N columns is generally sufficient, for some N, in which case you can just do something like:
f.write('%-40s %6s %10s %2s\n' % (filename, type, size, modified))
这篇关于用Python编写格式正确的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!