擅长:python、mysql、java
<p>我会使用这个新的(er)打印格式化程序(假设您的字段是一致的)。print/format语句非常容易使用,可以找到<a href="http://docs.python.org/2/library/string.html#formatstrings" rel="noreferrer">here</a>。由于您的数据可以被视为一个列表,因此您可以执行一次格式化调用,并提供正确的格式化程序数据,从而获得输出。与ljust或rjust相比,这有一点更细粒度的控制,但是有一个缺点,那就是您需要知道您输入的数据是一致的。</p>
<pre><code>with open(r'C:\path\to\logons.txt', 'r') as f:
for line in f:
data = line.split() # Splits on whitespace
print '{0[0]:<15}{0[1]:<15}{0[2]:<5}{0[3]:<15}{0[4]:>15}'.format(data)
</code></pre>