레이블이 text인 게시물을 표시합니다. 모든 게시물 표시
레이블이 text인 게시물을 표시합니다. 모든 게시물 표시

2017년 11월 26일 일요일

python, read txt line-by-line

list = open(FILENAME).read().splitlines()

만약 한글이 있어서,
UnicodeDecodeError: 'cp949' codec can't decode byte 0xa7 in position 55: illegal multibyte sequence
이라면, 다음으로 대체

import codecs
list = codecs.open(FILENAME, 'r', 'utf-8').read().splitlines()

그럼에도 아래와 같은 에러가 생긴다면
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb0 in position
다음으로 대체

'utf-8' -> 'euc-kr'

2016년 11월 16일 수요일

matlab text file read

문제
matlab에서 text file을 읽을 때, 처음 몇 줄을 skip하고 읽고 싶은 경우.
ex) in READ.txt
_____________________________
ID    ADDR     TEL(no blank)
int   char[]      int
1     abcd       012345
3     zzzz        012454
...
_____________________________

해결
textread()함수 이용
[id, addr, tel] = textread(strfilename, '%d %s %d', 'headerlines', 2);

기타 textscan, dlmread 들도 가능함.