格式化输出字符串,右对齐

问题描述 投票:0回答:8

我正在处理包含坐标 x、y、z 的文本文件

     1      128  1298039
123388        0        2
....

每行使用

分隔为 3 个项目
words = line.split()

处理数据后,我需要将坐标写回到另一个 txt 文件中,以便每列中的项目右对齐(以及输入文件)。每条线都是由坐标组成

line_new = words[0]  + '  ' + words[1]  + '  ' words[2].

C++ 中是否有类似

std::setw()
等的操纵器允许设置宽度和对齐方式?

python alignment string-formatting
8个回答
314
投票

使用较新的

str.format
语法尝试此方法:

line_new = '{:>12}  {:>12}  {:>12}'.format(word[0], word[1], word[2])

以下是如何使用旧的

%
语法(对于不支持
str.format
的旧版 Python 很有用):

line_new = '%12s  %12s  %12s' % (word[0], word[1], word[2])

78
投票

你可以这样对齐:

print('{:>8} {:>8} {:>8}'.format(*words))

其中

>
表示“右对齐”,8
 是特定值的
宽度

这是一个证明:

>>> for line in [[1, 128, 1298039], [123388, 0, 2]]: print('{:>8} {:>8} {:>8}'.format(*line)) 1 128 1298039 123388 0 2

诗。

*line

 表示 
line
 列表将被解包,因此 
.format(*line)
 的工作方式与 
.format(line[0], line[1], line[2])
 类似(假设 
line
 是仅包含三个元素的列表)。


74
投票
这是使用“f-string”格式进行格式化的另一种方法:

print( f"{'Trades:':<15}{cnt:>10}", f"{'Wins:':<15}{wins:>10}", f"{'Losses:':<15}{losses:>10}", f"{'Breakeven:':<15}{evens:>10}", f"{'Win/Loss Ratio:':<15}{win_r:>10}", f"{'Mean Win:':<15}{mean_w:>10}", f"{'Mean Loss:':<15}{mean_l:>10}", f"{'Mean:':<15}{mean_trd:>10}", f"{'Std Dev:':<15}{sd:>10}", f"{'Max Loss:':<15}{max_l:>10}", f"{'Max Win:':<15}{max_w:>10}", f"{'Sharpe Ratio:':<15}{sharpe_r:>10}", sep="\n" )
这将提供以下输出:

Trades: 2304 Wins: 1232 Losses: 1035 Breakeven: 37 Win/Loss Ratio: 1.19 Mean Win: 0.381 Mean Loss: -0.395 Mean: 0.026 Std Dev: 0.56 Max Loss: -3.406 Max Win: 4.09 Sharpe Ratio: 0.7395
您在这里所做的是说第一列长 15 个字符,左对齐,第二列(值)长 10 个字符,右对齐。

如果您从列表中加入项目并且想要格式化项目之间的空间,您可以使用 `` 和常规格式化技术。

此示例将每个数字用 3 个空格分隔。这里的关键是

f"{'':>3}"

print(f"{'':>3}".join(str(i) for i in range(1, 11)))
输出:

1 2 3 4 5 6 7 8 9 10
    

67
投票
可以通过使用

rjust

来实现:

line_new = word[0].rjust(10) + word[1].rjust(10) + word[2].rjust(10)
    

39
投票
我真的很喜欢 Python 3.6+ 中新的文字字符串插值:

line_new = f'{word[0]:>12} {word[1]:>12} {word[2]:>12}'

参考:

PEP 498 -- 文字字符串插值


7
投票
输出的简单列表:

a = 0.3333333 b = 200/3 print("variable a variable b") print("%10.2f %10.2f" % (a, b))

输出:

variable a variable b 0.33 66.67

%10.2f: 10 是最小长度,2 是小数位数。


7
投票
通过使用 f 字符串并控制尾随数字的数量来做到这一点:

print(f'A number -> {my_number:>20.5f}')
    

0
投票
将 Vlad 的精彩内容与其他内容混合,代码也可以编写为可读性和易用性,例如......

>>> cnt = wins = losses = str( 2) >>> evens = win_r = mean_w = str( 14) >>> mean_l = mean_trd = sd = str( 336) >>> max_l = max_w = sharpe_r = str(4278) >>> >>> rpad = 10 >>> >>> print( ... '\n Trades ' + cnt.rjust(rpad), ... '\n Wins ' + wins.rjust(rpad), ... '\n Losses ' + losses.rjust(rpad), ... '\n Breakeven ' + evens.rjust(rpad), ... '\n Win/Loss Ratio ' + win_r.rjust(rpad), ... '\n Mean Win ' + mean_w.rjust(rpad), ... '\n Mean Loss ' + mean_l.rjust(rpad), ... '\n Mean ' + mean_trd.rjust(rpad), ... '\n Std Dev ' + sd.rjust(rpad), ... '\n Max Loss ' + max_l.rjust(rpad), ... '\n Max Win ' + max_w.rjust(rpad), ... '\n Sharpe Ratio ' + sharpe_r.rjust(rpad), ... ) Trades 2 Wins 2 Losses 2 Breakeven 14 Win/Loss Ratio 14 Mean Win 14 Mean Loss 336 Mean 336 Std Dev 336 Max Loss 4278 Max Win 4278 Sharpe Ratio 4278
    
© www.soinside.com 2019 - 2024. All rights reserved.