과정에 오신 것을 환영합니다!

Python에서 데이터 가져오기 입문

Hugo Bowne-Anderson

Data Scientist at DataCamp

데이터 가져오기

  • 플랫 파일 (예: .txt, .csv)
  • 다른 소프트웨어의 파일

 

ch_1_1_v2.004.png

Python에서 데이터 가져오기 입문

데이터 가져오기

  • 플랫 파일 (예: .txt, .csv)
  • 다른 소프트웨어의 파일
  • 관계형 데이터베이스

ch_1_1_v2.005.png

Python에서 데이터 가져오기 입문

일반 텍스트 파일

ch_1_1_v2.007.png

Python에서 데이터 가져오기 입문

표 형식 데이터

titanic.csv

                        Name      Sex  Cabin  Survived
     Braund, Mr. Owen Harris     male    NaN         0
  Cumings, Mrs. John Bradley   female    C85         1
      Heikkinen, Miss. Laina   female    NaN         1
Futrelle, Mrs. Jacques Heath   female   C123         1
    Allen, Mr. William Henry     male    NaN         0
1 출처: Kaggle
Python에서 데이터 가져오기 입문

표 형식 데이터

titanic.csv

행이 강조 표시된 titanic.csv

Python에서 데이터 가져오기 입문

표 형식 데이터

titanic.csv

열이 강조 표시된 titanic.csv

  • 플랫 파일
Python에서 데이터 가져오기 입문

텍스트 파일 읽기

filename = 'huck_finn.txt'

file = open(filename, mode='r') # 'r' is to read
text = file.read()
file.close()
Python에서 데이터 가져오기 입문

텍스트 파일 출력

print(text)
YOU don't know about me without you have read a book by
the name of The Adventures of Tom Sawyer; but that 
ain't no matter. That book was made by Mr. Mark Twain,
and he told the truth, mainly. There was things which 
he stretched, but mainly he told the truth.  That is 
nothing. never seen anybody but lied one time or 
another, without it was Aunt Polly, or the widow, or 
maybe Mary. Aunt Polly--Tom's Aunt Polly, she is--and
Mary, and the Widow Douglas is all told about in that
book, which is mostly a true book, with some 
stretchers, as I said before.
Python에서 데이터 가져오기 입문

파일에 쓰기

filename = 'huck_finn.txt'
file = open(filename, mode='w')  # 'w' is to write
file.close()
Python에서 데이터 가져오기 입문

컨텍스트 매니저 with

with open('huck_finn.txt', 'r') as file:
    print(file.read())
YOU don't know about me without you have read a book by
the name of The Adventures of Tom Sawyer; but that 
ain't no matter. That book was made by Mr. Mark Twain,
and he told the truth, mainly. There was things which 
he stretched, but mainly he told the truth.  That is 
nothing. never seen anybody but lied one time or 
another, without it was Aunt Polly, or the widow, or 
maybe Mary. Aunt Polly--Tom's Aunt Polly, she is--and 
Mary, and the Widow Douglas is all told about in that 
book, which is mostly a true book, with some 
stretchers, as I said before.
Python에서 데이터 가져오기 입문

이번 실습에서 다룰 내용:

  • 파일을 콘솔에 출력
  • 특정 줄 출력
  • 플랫 파일 이해
Python에서 데이터 가져오기 입문

연습해 봅시다!

Python에서 데이터 가져오기 입문

Preparing Video For Download...