python右对齐格式化输出_Python的:格式化输出字符串,右对齐

1586010002-jmsa.png

I am processing a text file containing coordinates x,y,z

1 128 1298039

123388 0 2

....

every line is delimited into 3 items using

words = line.split ()

After processing data I need to write coordinates back in another txt file so as items in each column are aligned right (as well as the input file). Every line is composed of the coordinates

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

Is there any manipulator like std::setw ( ) etc. in C++ allowing to set the width and alignment?

解决方案

Try this approach using the newer str.format syntax:

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

And here's how to do it using the old % syntax (useful for older versions of Python that don't support str.format):

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值