반응형
파일에서 파이썬 (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
반응형
'파이썬' 카테고리의 다른 글
파이썬 AttributeError: 'module' object has no attribute 'urlretrieve' (0) | 2021.01.11 |
---|---|
파이썬에서 현재 모듈 내의 모든 클래스 목록을 어떻게 얻을 수 있습니까? (0) | 2021.01.11 |
파이썬 Flask-SQLAlchemy 앱에서 원시 SQL을 실행하는 방법 (0) | 2021.01.11 |
파이썬 동적 페이지를위한 스크래피가있는 셀레늄 (0) | 2021.01.10 |
파이썬 Pandas : Excel 파일에서 시트 목록 조회 (0) | 2021.01.10 |
댓글