csv파일을 다루는 연습을 시작했는데, csv파일을 읽을 때 reader 객체를 생성하는데, 속성값에서 delimiter라는 항목을 알게 되었다.
우선, delimiter란 무엇일까?
A delimiter is a sequence of one or more characters used to specify the boundary between separate, independent regions in plain text or other data streams. An example of a delimiter is the comma character, which acts as a field delimiter in a sequence of comma-separated values. (출처)
즉, 텍스트 또는 데이터에서 boundary를 명시하기위해 사용되는 하나 이상의 character의 sequence라는 것이다. 예를 들어 csv에서의 쉼표( , )가 있다.
음... 대충은 감이 잡힌다.
그러면 실제로는 어떻게 쓰이는지 알아보자.
다음으로, csv모듈의 reader()함수를 알아보자. (python 3.4 기준)
csv.
reader
(csvfile, dialect='excel', **fmtparams)Return a reader object which will iterate over lines in the given csvfile. csvfile can be any object which supports the iterator protocol and returns a string each time its __next__()
method is called — file objects and list objects are both suitable. If csvfile is a file object, it should be opened with newline=''
. [1] An optional dialect parameter can be given which is used to define a set of parameters specific to a particular CSV dialect. It may be an instance of a subclass of the Dialect
class or one of the strings returned by the list_dialects()
function. The other optional fmtparams keyword arguments can be given to override individual formatting parameters in the current dialect.
주어진 csv파일 line별로 iterate하는 reader 객체를 반환한다.
그러한 객체는 __next__()메소드를 사용하여 string을 반환한다.
csv파일이 파일 객체로 생성되면, newline=''을 사용하여 open되어야한다. (위의 코드에서 4,5번줄 참고)
'공부 > 16산학프로젝트' 카테고리의 다른 글
[16산학프로젝트/Python]엑셀파일의 특정 행을 csv파일에 저장하기 (0) | 2017.02.27 |
---|---|
[16산학프로젝트] 봇은 어떻게 웹과 모바일 앱을 무너뜨릴 수 있을까 (0) | 2017.02.27 |
[16산학프로젝트/Python]HTML 태그 제거하기 (0) | 2017.02.21 |
[16산학프로젝트/Python]3주차 (0) | 2017.02.21 |
[16산학프로젝트/Python]텍스트파일을 csv파일로 바꾸기 (0) | 2017.02.15 |