简体   繁体   English

将 numpy 保存到文本文件中

[英]Saving numpy into a text file

Trying to save numpy into a new txt file.试图将 numpy 保存到一个新的 txt 文件中。 I've got the amount, average, min and max.我有数量,平均值,最小值和最大值。 I'm now trying to save those numbers into a new txt file.我现在试图将这些数字保存到一个新的 txt 文件中。 I'm new to numpy and python, so this might be an easy question.我是 numpy 和 python 的新手,所以这可能是一个简单的问题。

import numpy as np

def main():
    x = np.loadtxt("wind_readings.txt")
    print("There are", len(x), "")
    print('Average:', np.average(x))
    print('Max:', np.amax(x))
    print('Min:', np.amin(x))

main()

I want the new txt file to have this format:我希望新的 txt 文件具有以下格式:

Amount:数量:

Average:平均数:

Max:最大限度:

Min:最小值:

You can try你可以试试

file = open("testfile.txt","w") 
file.write(f"Amount: {len(x)}\n")
file.write(f"Average: {np.average(x)}\n")
file.write(f"Max: {np.amax(x)}\n")
file.write(f"Min: {np.amin(x)}\n")
file.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM