gpt4 book ai didi

python - 在python中导入一个数组

转载 作者:太空狗 更新时间:2023-10-29 17:23:35 24 4
gpt4 key购买 nike

我如何从文件中将数组导入 python (numpy.arry),如果文件不存在,则必须写入该文件。

例如,将矩阵保存到文件中,然后将其加载回来。

最佳答案

检查 numpy example list 上的条目.这是 .loadtxt() 上的条目

>>> from numpy import *
>>>
>>> data = loadtxt("myfile.txt") # myfile.txt contains 4 columns of numbers
>>> t,z = data[:,0], data[:,3] # data is 2D numpy array
>>>
>>> t,x,y,z = loadtxt("myfile.txt", unpack=True) # to unpack all columns
>>> t,z = loadtxt("myfile.txt", usecols = (0,3), unpack=True) # to select just a few columns
>>> data = loadtxt("myfile.txt", skiprows = 7) # to skip 7 rows from top of file
>>> data = loadtxt("myfile.txt", comments = '!') # use '!' as comment char instead of '#'
>>> data = loadtxt("myfile.txt", delimiter=';') # use ';' as column separator instead of whitespace
>>> data = loadtxt("myfile.txt", dtype = int) # file contains integers instead of floats

关于python - 在python中导入一个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1796597/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com