我正在处理包含坐标 x、y、z 的文本文件
1 128 1298039
123388 0 2
....
每行被分隔为 3 个项目,使用
words = line.split()
处理数据后,我需要将坐标写回到另一个 txt 文件中,以便每列中的项目右对齐(以及输入文件)。每条线由坐标组成
line_new = words[0] + ' ' + words[1] + ' ' words[2].
有没有类似的操纵器std::setw()
等在C++中允许设置宽度和对齐方式吗?
使用较新的方法尝试此方法str.format syntax https://docs.python.org/3/library/string.html#formatstrings:
line_new = '{:>12} {:>12} {:>12}'.format(word[0], word[1], word[2])
这是使用旧方法的方法%
语法(对于不支持的旧版 Python 很有用str.format
):
line_new = '%12s %12s %12s' % (word[0], word[1], word[2])
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)