【问题标题】:How can I save file by using savetxt in the python?如何在 python 中使用 savetxt 保存文件?
【发布时间】:2023-02-03 10:45:08
【问题描述】:

我有数据列表,例如

a = [1,2,3,4,5]
b = [6,7,8,9,0]
c = [0,0,0,0,0]

我想将此数据以以下形式保存到文件中

1 6 0

2 7 0

3 8 0

4 9 0

5 0 0

我可以通过使用“打开”来做到这一点。但我想使用 savetxt。

如何使用savetxt将数据保存到上面的表格中?

【问题讨论】:

标签: python numpy file-io


【解决方案1】:

也许你可以试试这个: [np.r][1]

# a, b c are your lists
#
# save to data.txt file       and format is integer.
#
np.savetxt('data.txt',np.r_['1,2,0', a,b,c], fmt='%i')

# open data.txt

1 6 0
2 7 0
3 8 0
4 9 0
5 0 0


  [1]: https://numpy.org/doc/stable/reference/generated/numpy.r_.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-12
    • 1970-01-01
    • 1970-01-01
    • 2014-09-01
    • 2020-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多