본문 바로가기
파이썬

파이썬에서 배열 가져 오기

by º기록 2021. 1. 11.
반응형

파일에서 파이썬 (numpy.array)으로 배열을 가져 오려면 어떻게해야하며 파일이 아직 존재하지 않으면 작성해야합니다.

예를 들어 행렬을 파일에 저장 한 다음 다시로드합니다.

 

해결 방법

 


>>> 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

 

참조 페이지 https://stackoverflow.com/questions/1796597

 

 

반응형

댓글