【问题标题】:How to set vertical the ouput in python?如何在python中设置垂直输出?
【发布时间】:2021-02-25 10:47:19
【问题描述】:

我想在 python 中创建一个 `.txt 格式的输出。问题是结果是水平显示的。

如何垂直设置我的值?

例如,我的输出格式为:

[6.48421466 4.28001787 6.76134729 5.45509747 7.68957008 3.25036695
 5.26088052 4.45128821 3.99247354 4.04626299 3.19329872 5.21477152
 5.75235725 5.4032342  4.75781543 4.86203242 4.94567754 6.2735008
 3.90191443 3.34211125 4.80198239 5.39782033 4.65575587 4.09630464
 4.68439523 4.24076152 2.70145788 3.18283344 2.67654271 6.71627663
 3.99750959 4.55231039 6.57358438 4.59699555 3.37902555 4.60574622
 5.7602282  5.34084772 4.2033163  4.41813674 5.83988272 4.56814295
 4.22884378 3.75609531 4.54537646 4.82880385 4.4317394  4.69930332
 5.46046878 3.38346653 4.02209524 4.73886735 4.91038119 3.83070474
 3.46198489 4.89056201 3.45052842 3.60843658 5.38378215 5.82383583
 3.37329096 3.72459568 5.42039616 5.4329635  5.16597499 3.61643261
 5.51898447 4.75482025 4.43989681 4.71631944 5.04887236 4.16837725]

我试过了:

with open('outputdata.txt','w',encoding='utf-8') as fout:
     fout.writelines(str(A))

with open('outputdata.txt','w',encoding='utf-8') as fout:
     fout.write(str(A)+'\n')

但这没有用。我的结果是水平显示的,而不是垂直显示的(我的意思是列格式)

【问题讨论】:

  • 您正在输出列表的字符串表示形式。如果您希望列表中的每个条目在单独的行上输出(并且没有 [ 括号 ]),您可以遍历列表中的每个项目并编写它。
  • 对于第二个建议命令错误显示:TypeError: write() argument must be str, not numpy.float64 。至于你的第一个建议,我不明白该怎么做……你能解释一下吗?

标签: python file output write


【解决方案1】:

除非您手动将每个值写入不同的行,否则写入文件将始终是水平的。 我建议你使用这个简单的 foreach 循环:

with open('outputdata.txt','w') as fout:
    foreach item in A:
        fout.write(str(item) + "\n")

当然,如果需要,您可以在开头添加“[”,在末尾添加“]”。 祝你好运!

【讨论】:

    【解决方案2】:

    我认为这是您的数据:

    a = [6.48421466, 4.28001787, 6.76134729, 5.45509747, 7.68957008, 3.25036695, 5.26088052, 4.45128821, 3.99247354, 4.04626299, 3.19329872, 5.21477152, 5.75235725, 5.4032342, 4.75781543, 4.86203242, 4.94567754, 6.2735008, 3.90191443, 3.34211125, 4.80198239, 5.39782033, 4.65575587, 4.09630464, 4.68439523, 4.24076152, 2.70145788, 3.18283344, 2.67654271, 6.71627663, 3.99750959, 4.55231039, 6.57358438, 4.59699555, 3.37902555, 4.60574622, 5.7602282, 5.34084772, 4.2033163, 4.41813674, 5.83988272, 4.56814295, 4.22884378, 3.75609531, 4.54537646, 4.82880385, 4.4317394, 4.69930332, 5.46046878, 3.38346653, 4.02209524, 4.73886735, 4.91038119, 3.83070474, 3.46198489, 4.89056201, 3.45052842, 3.60843658, 5.38378215, 5.82383583, 3.37329096, 3.72459568, 5.42039616, 5.4329635, 5.16597499, 3.61643261, 5.51898447, 4.75482025, 4.43989681, 4.71631944, 5.04887236, 4.16837725]
    

    你可以使用:

    with open('outputdata.txt', 'w',) as f:
        f.writelines([str(x) + '\n' for x in a])
    

    导致

    6.48421466
    4.28001787
    6.76134729
    5.45509747
    7.68957008
    3.25036695
    5.26088052
    4.45128821
    3.99247354
    [...]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-26
      • 2015-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-17
      • 1970-01-01
      • 2016-10-21
      相关资源
      最近更新 更多