Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

Python: อ่าน Text File เข้า List ด้วย split()

การจัดเก็บข้อมูลในไฟล์ช่วยเก็บบันทึกข้อมูลที่โปรแกรมทำงาน หมายความว่าไม่ต้อง generate data ใหม่ทุกครั้ง แค่อ่านจากไฟล์

อ่านไฟล์ใช้เมธอด readlines() อ่านเสร็จ ใช้ split() แปลงบรรทัดเหล่านั้นเป็น list

คู่มือนี้อธิบายวิธีใช้ split() อ่าน text file เข้า list พร้อมตัวอย่าง เริ่มได้เลย

Python: อ่าน Text File เข้า List

เริ่มจาก text file ชื่อ grilled_cheese.txt มีส่วนผสมทำแซนด์วิชชีสอบ ไฟล์เป็นแบบนี้:

2 tbsp, ricotta
1 tbsp, grated parmesan
50g, mozzarella
25g, gorgonzola
2, thick slices white bread
1 tbsp, butter

คอลัมน์แรกคือปริมาณส่วนผสม คอลัมน์ที่สองคือชื่อส่วนผสม

อ่านไฟล์เข้าโค้ดด้วย open() และ readlines():

with open("grilled_cheese.txt", "r") as grilled_cheese:
    lines = grilled_cheese.readlines()
    print(lines)

โค้ดเปิด "grilled_cheese.txt" ใน read mode ("r" ใน open()) แล้วพิมพ์บรรทัดเหล่านั้น

ดูผลลัพธ์:

['2 tbsp, ricotta\n', '1 tbsp, grated parmesan\n', '50g, mozzarella\n', '25g, gorgonzola\n', '2, thick slices white bread\n', '1 tbsp, butter\n']

โค้ดคืน list ของแต่ละบรรทัดในไฟล์ ยังไม่ใช่ output ที่คาดหวัง แม้อ่านเข้า list แล้ว แต่มีปัญหา: แต่ละบรรทัดเก็บใน string เดียว ปริมาณและชื่อส่วนผสมไม่แยกกัน

แบ่งค่าเข้า List (Divide Values into a List)

แก้ปัญหานี้ใช้ split() method แบ่ง string ด้วย separator character ที่ระบุ

เริ่มประกาศ 2 ลิสต์: quantities และ ingredients โค้ดนี้ยัง indent อยู่ เพราะเป็นส่วนของ open() block

quantities = []
ingredients = []

วนลูป list เพื่อเข้าถึงแต่ละบรรทัด แล้ว split แต่ละบรรทัด 2 ส่วน จุดแบ่งคือ comma ตามด้วย space (", "):

for l in lines:
    as_list = l.split(", ")
    quantities.append(as_list[0])
    ingredients.append(as_list[1])

for loop อ่านไฟล์ทีละบรรทัด ค่าแรกใน "as_list" คือปริมาณ ค่าที่สองคือชื่อส่วนผสม เพิ่มเข้า list ตามลำดับ แล้วพิมพ์ทั้ง 2 list:

print(quantities)
print(ingredients)

รันโค้ด:

['2 tbsp, ricotta\n', '1 tbsp, grated parmesan\n', '50g, mozzarella\n', '25g, gorgonzola\n', '2, thick slices white bread\n', '1 tbsp, butter\n']
['2 tbsp', '1 tbsp', '50g', '25g', '2', '1 tbsp']
['ricotta\n', 'grated parmesan\n', 'mozzarella\n', 'gorgonzola\n', 'thick slices white bread\n', 'butter\n']

โค้ดพิมพ์ 3 lists: 1) ทุกบรรทัดในไฟล์ 2) ปริมาณทั้งหมด 3) ส่วนผสมทั้งหมด

ลบ New Lines (Remove New Lines)

ยังมีจุดปรับปรุง: ทุกส่วนผสมลงท้ายด้วย "\n" (new line character) ลบได้ด้วย replace() method:

for l in lines:
    as_list = l.split(", ")
    quantities.append(as_list[0])
    ingredients.append(as_list[1].replace("\n", ""))

ใน for loop แทนที่ "\n" ด้วย empty string ทำที่ as_list[1] ซึ่งสัมพันธ์กับชื่อส่วนผสม

โปรแกรมพร้อมแล้ว:

with open("grilled_cheese.txt", "r") as grilled_cheese:
    lines = grilled_cheese.readlines()
    quantities = []
    ingredients = []
    for l in lines:
        as_list = l.split(", ")
        quantities.append(as_list[0])
        ingredients.append(as_list[1].replace("\n", ""))
    print(quantities)
    print(ingredients)

รันโค้ด:

['2 tbsp', '1 tbsp', '50g', '25g', '2', '1 tbsp']
['ricotta', 'grated parmesan', 'mozzarella', 'gorgonzola', 'thick slices white bread', 'butter']

โค้ดแปลง text file เป็น 2 lists สำเร็จ: list หนึ่งมีปริมาณ ส่วนหนึ่งมีส่วนผสม

สรุป

อ่าน text file 用 open() และ readlines() อ่านเข้า list 用 split() แบ่ง string เป็น list ที่ character ที่กำหนด

ตัวอย่างข้างบน split string เป็น list ที่ comma และ space (", ") ตอนนี้คุณพร้อมอ่าน text file เข้า list ใน Python อย่าง expert แล้ว