ยินดีต้อนรับสู่คอร์ส!

Python เบื้องต้น: การนำเข้าข้อมูล

Hugo Bowne-Anderson

Data Scientist at DataCamp

นำเข้าข้อมูล

  • Flat files เช่น .txt, .csv
  • ไฟล์จากซอฟต์แวร์อื่น

 

ch_1_1_v2.004.png

Python เบื้องต้น: การนำเข้าข้อมูล

นำเข้าข้อมูล

  • Flat files เช่น .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 โดยไฮไลต์คอลัมน์หนึ่งคอลัมน์

  • Flat file
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 เบื้องต้น: การนำเข้าข้อมูล

Context manager กับ 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 เบื้องต้น: การนำเข้าข้อมูล

ในแบบฝึกหัด คุณจะได้:

  • พิมพ์ไฟล์ออกทาง console
  • พิมพ์บรรทัดที่ต้องการ
  • ทำความเข้าใจ flat files
Python เบื้องต้น: การนำเข้าข้อมูล

มาฝึกกันเถอะ!

Python เบื้องต้น: การนำเข้าข้อมูล

Preparing Video For Download...