Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python—格式化ascii表的数字_Python_Python 3.x_Formatting_Numbers_Text Files - Fatal编程技术网

Python—格式化ascii表的数字

Python—格式化ascii表的数字,python,python-3.x,formatting,numbers,text-files,Python,Python 3.x,Formatting,Numbers,Text Files,我想知道为什么我的代码不起作用。我想用小数点后的两个数字对两列数据进行四舍五入。我现在遵循以下代码: from __future__ import print_function with open('input.dat', 'r') as f, open('output.txt', 'w') as outfile: for line in f: try: line = line.strip() columns = line

我想知道为什么我的代码不起作用。我想用小数点后的两个数字对两列数据进行四舍五入。我现在遵循以下代码:

from __future__ import print_function

with open('input.dat', 'r') as f, open('output.txt', 'w') as outfile:
    for line in f:
        try:
            line = line.strip()
            columns = line.split()
            vx = float(columns[0])
            vy = float(columns[1])
            print("{:.2f\t}".format(vx),"{:.2f}".format(vy), file=outfile)
        except ValueError:
            print(line, file=outfile)
输入数据如下所示

XY
HH
M&M
TS
1.83746 2.12131
1.12121 1.89942
1.32435 1.99443
1.65392 2.00001
1.48732 2.21773
...
...
输出应如下所示:

XY
HH
M&M
TS
1.84    2.12
1.12    1.90
1.32    1.99
1.65    2.00
1.49    2.22
...
...

我认为这条线错了

print("{:.2f\t}".format(vx),"{:.2f}".format(vy), file=outfile)
选项卡应该在
{}
之外,我会这样重写它

print("{:.2f}\t{:.2f}".format(vx, vy), file=outfile)

我认为这条线错了

print("{:.2f\t}".format(vx),"{:.2f}".format(vy), file=outfile)
选项卡应该在
{}
之外,我会这样重写它

print("{:.2f}\t{:.2f}".format(vx, vy), file=outfile)