Python đại diện cho phần cuối của dòng khi đọc tệp như thế nào?

Một tệp văn bản có thể được coi là một chuỗi các dòng, giống như một chuỗi Python có thể được coi là một chuỗi ký tự. Ví dụ: đây là mẫu tệp văn bản ghi lại hoạt động thư từ nhiều cá nhân khác nhau trong nhóm phát triển dự án nguồn mở

From [email protected] Sat Jan  5 09:14:16 2008
Return-Path: 
Date: Sat, 5 Jan 2008 09:12:18 -0500
To: [email protected]
From: [email protected]
Subject: [sakai] svn commit: r39772 - content/branches/
Details: http://source.sakaiproject.org/viewsvn/?view=rev&rev=39772
...

Toàn bộ tệp tương tác thư có sẵn từ

www. py4e. com/code3/mbox. txt

và một phiên bản rút gọn của tệp có sẵn từ

www. py4e. com/code3/mbox-ngắn. txt

Các tệp này ở định dạng chuẩn cho một tệp chứa nhiều thư. Các dòng bắt đầu bằng “Từ” phân tách các thư và các dòng bắt đầu bằng “Từ. ” là một phần của tin nhắn. Để biết thêm thông tin về định dạng mbox, xem https. // vi. wikipedia. tổ chức/wiki/Mbox

Để ngắt tệp thành các dòng, có một ký tự đặc biệt đại diện cho “cuối dòng” được gọi là ký tự xuống dòng

Trong Python, chúng tôi biểu thị ký tự xuống dòng dưới dạng dấu gạch chéo ngược-n trong hằng chuỗi. Mặc dù đây trông giống như hai ký tự, nhưng nó thực sự là một ký tự duy nhất. Khi chúng ta xem xét biến bằng cách nhập “stuff” vào trình thông dịch, nó sẽ hiển thị cho chúng ta

#Create a variable for the file name
filename = 'Plates_output_simple.csv'

#Open the file
infile = open(filename, 'r') 

#Print the first two lines of the file
print(infile.readline())
print(infile.readline())

#call infile.read()
print(infile.read())

#close the file
infile.close()
4 trong chuỗi, nhưng khi chúng ta sử dụng
#Create a variable for the file name
filename = 'Plates_output_simple.csv'

#Open the file
infile = open(filename, 'r') 

#Print the first two lines of the file
print(infile.readline())
print(infile.readline())

#call infile.read()
print(infile.read())

#close the file
infile.close()
5 để hiển thị chuỗi, chúng ta sẽ thấy chuỗi được chia thành hai dòng bởi ký tự xuống dòng

Bạn cũng có thể thấy rằng độ dài của chuỗi

#Create a variable for the file name
filename = 'Plates_output_simple.csv'

#Open the file
infile = open(filename, 'r') 

#Print the first two lines of the file
print(infile.readline())
print(infile.readline())

#call infile.read()
print(infile.read())

#close the file
infile.close()
6 là ba ký tự vì ký tự xuống dòng là ký tự đơn

Vì vậy, khi chúng ta nhìn vào các dòng trong một tệp, chúng ta cần nhớ rằng có một ký tự vô hình đặc biệt được gọi là dòng mới ở cuối mỗi dòng đánh dấu sự kết thúc của dòng

Hiểu sự khác biệt giữa tên tệp, đối tượng tệp đã mở và dữ liệu được đọc từ tệp

  • Có thể ghi đầu ra vào một tệp văn bản với định dạng đơn giản

  • Tại sao chúng ta muốn đọc và ghi tệp?

    Khả năng mở và đọc trong các tệp cho phép chúng tôi làm việc với các tập dữ liệu lớn hơn, nơi không thể nhập từng giá trị và lưu trữ chúng một lần dưới dạng biến. Ghi tệp cho phép chúng tôi xử lý dữ liệu của mình và sau đó lưu kết quả đầu ra vào tệp để chúng tôi có thể xem sau

    Ngay bây giờ, chúng ta sẽ thực hành làm việc với tệp văn bản được phân cách bằng dấu phẩy (. csv) có chứa một số cột dữ liệu. Tuy nhiên, những gì bạn học được trong bài học này có thể được áp dụng cho bất kỳ tệp văn bản chung nào. Trong bài học tiếp theo, bạn sẽ học một cách khác để đọc và xử lý. dữ liệu csv

    Đường dẫn đến tệp

    Để mở một tệp, chúng ta cần cho Python biết chính xác vị trí của tệp, liên quan đến nơi Python hiện đang làm việc (thư mục làm việc). Trong Spyder, chúng tôi có thể làm điều này bằng cách đặt thư mục làm việc hiện tại của chúng tôi thành thư mục chứa tệp. Hoặc, khi chúng tôi cung cấp tên tệp, chúng tôi có thể cung cấp đường dẫn đầy đủ tới tệp

    Thiết lập bài học

    Chúng ta sẽ làm việc với file thực hành Plates_output_simple. csv

    1. Xác định vị trí tệp Plates_output_simple. csv trong thư mục home/Desktop/workshops/bash-git-python
    2. Sao chép tệp vào thư mục làm việc của bạn, home/Desktop/workshops/YourName
    3. Đảm bảo rằng thư mục làm việc của bạn cũng được đặt thành thư mục home/Desktop/workshops/YourName
    4. Khi bạn đang làm việc, hãy đảm bảo rằng bạn đã lưu (các) tập lệnh mở tệp của mình vào thư mục này

    Thiết lập tệp

    Hãy mở và kiểm tra cấu trúc của tệp Plates_output_simple. csv. Nếu bạn mở tệp trong trình soạn thảo văn bản, bạn sẽ thấy tệp chứa nhiều dòng văn bản

    Python đại diện cho phần cuối của dòng khi đọc tệp như thế nào?

    Tuy nhiên, điều này là khá khó đọc. Nếu bạn mở tệp trong một chương trình bảng tính chẳng hạn như LibreOfficeCalc hoặc Excel, bạn có thể thấy rằng tệp được sắp xếp thành các cột, với mỗi cột được phân tách bằng dấu phẩy trong hình trên (do đó có phần mở rộng tệp. csv, viết tắt của các giá trị được phân tách bằng dấu phẩy)

    Python đại diện cho phần cuối của dòng khi đọc tệp như thế nào?

    Tệp chứa một hàng tiêu đề, theo sau là tám hàng dữ liệu. Mỗi hàng đại diện cho một hình ảnh tấm duy nhất. Nếu chúng ta nhìn vào các tiêu đề cột, chúng ta có thể thấy rằng chúng ta đã thu thập dữ liệu cho từng tấm

    • Tên của hình ảnh mà dữ liệu được thu thập
    • Biển số (có 4 tấm, mỗi tấm được chụp ở 2 thời điểm khác nhau)
    • Điều kiện tăng trưởng (kiểm soát hoặc thử nghiệm)
    • Mốc thời gian quan sát (24 hoặc 48 giờ)
    • Đếm khuẩn lạc trên đĩa
    • Kích thước khuẩn lạc trung bình của đĩa
    • Tỷ lệ đĩa được bao phủ bởi các khuẩn lạc vi khuẩn

    Chúng tôi sẽ đọc trong tệp dữ liệu này và sau đó làm việc để phân tích dữ liệu

    Mở và đọc tệp là một quy trình gồm ba bước

    Chúng tôi sẽ mở và đọc tệp theo ba bước

    1. Chúng ta sẽ tạo một biến để chứa tên của tệp mà chúng ta muốn mở
    2. Ta sẽ gọi open để mở file
    3. Chúng ta sẽ gọi một hàm để thực sự đọc dữ liệu trong tệp và lưu trữ nó trong một biến để chúng ta có thể xử lý nó

    Và sau đó, còn một bước nữa để làm

    • Khi chúng ta hoàn thành, chúng ta nên nhớ đóng tệp

    Bạn có thể coi ba bước này tương tự như việc mượn một cuốn sách từ thư viện. Đầu tiên, bạn phải vào danh mục hoặc cơ sở dữ liệu để tìm cuốn sách bạn cần (tên tệp). Sau đó, bạn phải lấy nó ra khỏi giá và mở sách lên (chức năng mở). Cuối cùng, để có được bất kỳ thông tin nào từ cuốn sách, bạn phải đọc các từ (chức năng đọc)

    Dưới đây là một ví dụ về mở, đọc và đóng tệp

    #Create a variable for the file name
    filename = 'Plates_output_simple.csv' #This is simply a string of text
    
    #Open the file
    infile = open(filename, 'r') # 'r' says we are opening the file to read, infile is the opened file object that we will read from
    
    #Store the data from the file in a variable
    data = infile.read()
    
    #Print the data in the file
    print(data)
    
    #close the file
    infile.close()
    

    Khi chúng tôi đã đọc dữ liệu trong tệp thành dữ liệu biến của mình, chúng tôi có thể coi nó như bất kỳ biến nào khác trong mã của chúng tôi

    Sử dụng tên nhất quán để làm cho mã của bạn rõ ràng hơn

    Bạn nên phát triển một số thói quen nhất quán về cách bạn mở và đọc tệp. Sử dụng cùng (hoặc tương tự. ) đặt tên biến mỗi lần sẽ giúp bạn dễ dàng theo dõi biến nào là tên tệp, biến nào là đối tượng tệp được mở và biến nào chứa dữ liệu đọc vào

    Trong các ví dụ này, chúng tôi sẽ sử dụng

    #Create a variable for the file name
    filename = 'Plates_output_simple.csv'
    
    #Open the file
    infile = open(filename, 'r') 
    
    #Print the first two lines of the file
    print(infile.readline())
    print(infile.readline())
    
    #call infile.read()
    print(infile.read())
    
    #close the file
    infile.close()
    
    3 cho chuỗi văn bản chứa tên tệp,
    #Create a variable for the file name
    filename = 'Plates_output_simple.csv'
    
    #Open the file
    infile = open(filename, 'r') 
    
    #Print the first two lines of the file
    print(infile.readline())
    print(infile.readline())
    
    #call infile.read()
    print(infile.read())
    
    #close the file
    infile.close()
    
    4 cho đối tượng tệp đang mở mà từ đó chúng tôi có thể đọc dữ liệu và
    #Create a variable for the file name
    filename = 'Plates_output_simple.csv'
    
    #Open the file
    infile = open(filename, 'r') 
    
    #Print the first two lines of the file
    print(infile.readline())
    print(infile.readline())
    
    #call infile.read()
    print(infile.read())
    
    #close the file
    infile.close()
    
    5 cho biến chứa nội dung của tệp

    Các lệnh đọc trong tệp

    Có nhiều lệnh cho phép chúng ta đọc dữ liệu từ tệp.
    ______26 sẽ đọc toàn bộ tệp dưới dạng một chuỗi văn bản.
    ______27 sẽ đọc từng dòng một (mỗi lần bạn gọi lệnh này, lệnh này sẽ đọc ở dòng tiếp theo).
    ______28 sẽ đọc tất cả các dòng vào một danh sách, trong đó mỗi dòng của tệp là một mục trong danh sách.

    Trộn các lệnh này có thể có một số kết quả không mong muốn

    #Create a variable for the file name
    filename = 'Plates_output_simple.csv'
    
    #Open the file
    infile = open(filename, 'r') 
    
    #Print the first two lines of the file
    print(infile.readline())
    print(infile.readline())
    
    #call infile.read()
    print(infile.read())
    
    #close the file
    infile.close()
    

    Lưu ý rằng lệnh

    #Create a variable for the file name
    filename = 'Plates_output_simple.csv'
    
    #Open the file
    infile = open(filename, 'r') 
    
    #Print the first two lines of the file
    print(infile.readline())
    print(infile.readline())
    
    #call infile.read()
    print(infile.read())
    
    #close the file
    infile.close()
    
    6 bắt đầu ở dòng thứ ba của tệp, nơi hai lệnh
    #Create a variable for the file name
    filename = 'Plates_output_simple.csv'
    
    #Open the file
    infile = open(filename, 'r') 
    
    #Print the first two lines of the file
    print(infile.readline())
    print(infile.readline())
    
    #call infile.read()
    print(infile.read())
    
    #close the file
    infile.close()
    
    7 đầu tiên bị bỏ dở

    Hãy nghĩ về nó như thế này. khi tệp được mở, một con trỏ được đặt ở góc trên cùng bên trái của tệp ở đầu dòng đầu tiên. Bất cứ khi nào một chức năng đọc được gọi, con trỏ hoặc con trỏ sẽ tiến lên từ vị trí của nó.

    #Create a variable for the file name
    filename = 'Plates_output_simple.csv'
    
    #Open the file
    infile = open(filename, 'r') 
    
    #Print the first two lines of the file
    print(infile.readline())
    print(infile.readline())
    
    #call infile.read()
    print(infile.read())
    
    #close the file
    infile.close()
    
    7 đầu tiên bắt đầu ở đầu tệp và nâng cao đến cuối dòng đầu tiên. Bây giờ, con trỏ được định vị ở đầu dòng thứ hai.
    #Create a variable for the file name
    filename = 'Plates_output_simple.csv'
    
    #Open the file
    infile = open(filename, 'r') 
    
    #Print the first two lines of the file
    print(infile.readline())
    print(infile.readline())
    
    #call infile.read()
    print(infile.read())
    
    #close the file
    infile.close()
    
    7 thứ hai tiến đến cuối dòng thứ hai của tệp và để con trỏ ở vị trí đầu dòng thứ ba.
    #Create a variable for the file name
    filename = 'Plates_output_simple.csv'
    
    #Open the file
    infile = open(filename, 'r') 
    
    #Print the first two lines of the file
    print(infile.readline())
    print(infile.readline())
    
    #call infile.read()
    print(infile.read())
    
    #close the file
    infile.close()
    
    6 bắt đầu từ vị trí này và tiến dần đến cuối tệp

    Nói chung, nếu bạn muốn chuyển đổi giữa các loại lệnh đọc khác nhau, bạn nên đóng tệp rồi mở lại để bắt đầu lại

    Đọc tất cả các dòng của một tệp vào một danh sách

    #Create a variable for the file name
    filename = 'Plates_output_simple.csv'
    
    #Open the file
    infile = open(filename, 'r') 
    
    #Print the first two lines of the file
    print(infile.readline())
    print(infile.readline())
    
    #call infile.read()
    print(infile.read())
    
    #close the file
    infile.close()
    
    8 sẽ đọc tất cả các dòng thành một danh sách, trong đó mỗi dòng của tệp là một mục trong danh sách. Điều này cực kỳ hữu ích, bởi vì một khi chúng tôi đã đọc tệp theo cách này, chúng tôi có thể lặp qua từng dòng của tệp và xử lý nó. Cách tiếp cận này hoạt động tốt trên các tệp dữ liệu nơi dữ liệu được sắp xếp thành các cột tương tự như bảng tính, vì có khả năng chúng tôi sẽ muốn xử lý từng dòng theo cùng một cách

    Ví dụ dưới đây minh họa cách tiếp cận này

    #Create a variable for the file name
    filename = 'Plates_output_simple.csv' #This is simply a string of text
    
    #Open the file
    infile = open(filename, 'r') # 'r' says we are opening the file to read, infile is the opened file object that we will read from
    
    #Store the data from the file in a variable
    data = infile.read()
    
    #Print the data in the file
    print(data)
    
    #close the file
    infile.close()
    
    5

    Sử dụng #Create a variable for the file name filename = 'Plates_output_simple.csv' #This is simply a string of text #Open the file infile = open(filename, 'r') # 'r' says we are opening the file to read, infile is the opened file object that we will read from #Store the data from the file in a variable data = infile.read() #Print the data in the file print(data) #close the file infile.close() 55 để phân tách các “cột”

    Vì dữ liệu của chúng tôi nằm trong một. csv, chúng ta có thể sử dụng lệnh

    #Create a variable for the file name
    filename = 'Plates_output_simple.csv' #This is simply a string of text
    
    #Open the file
    infile = open(filename, 'r') # 'r' says we are opening the file to read, infile is the opened file object that we will read from
    
    #Store the data from the file in a variable
    data = infile.read()
    
    #Print the data in the file
    print(data)
    
    #close the file
    infile.close()
    
    56 để tách từng dòng của tệp thành một danh sách. Điều này có thể hữu ích nếu chúng tôi muốn truy cập các cột cụ thể của tệp

    #Create a variable for the file name
    filename = 'Plates_output_simple.csv' #This is simply a string of text
    
    #Open the file
    infile = open(filename, 'r') # 'r' says we are opening the file to read, infile is the opened file object that we will read from
    
    #Store the data from the file in a variable
    data = infile.read()
    
    #Print the data in the file
    print(data)
    
    #close the file
    infile.close()
    
    8

    Tên nhất quán, một lần nữa

    Thoạt nhìn, tên biến

    #Create a variable for the file name
    filename = 'Plates_output_simple.csv' #This is simply a string of text
    
    #Open the file
    infile = open(filename, 'r') # 'r' says we are opening the file to read, infile is the opened file object that we will read from
    
    #Store the data from the file in a variable
    data = infile.read()
    
    #Print the data in the file
    print(data)
    
    #close the file
    infile.close()
    
    57 trong ví dụ trên có thể không có nhiều ý nghĩa. Trên thực tế, chúng tôi đã chọn nó là viết tắt của “split line”, mô tả chính xác nội dung của biến

    Bạn không cần phải sử dụng quy ước đặt tên này nếu không muốn, nhưng bạn nên cố gắng sử dụng các tên biến nhất quán trong mã của mình cho các hoạt động phổ biến như thế này. Nó sẽ giúp mở một tập lệnh cũ dễ dàng hơn nhiều và nhanh chóng hiểu chính xác những gì nó đang làm

    Chuyển đổi văn bản thành số

    Khi chúng ta gọi lệnh

    #Create a variable for the file name
    filename = 'Plates_output_simple.csv' #This is simply a string of text
    
    #Open the file
    infile = open(filename, 'r') # 'r' says we are opening the file to read, infile is the opened file object that we will read from
    
    #Store the data from the file in a variable
    data = infile.read()
    
    #Print the data in the file
    print(data)
    
    #close the file
    infile.close()
    
    58 trong đoạn mã trước, Python đọc nội dung của tệp dưới dạng một chuỗi. Nếu chúng tôi muốn mã của mình nhận ra thứ gì đó trong tệp dưới dạng số, chúng tôi cần nói với nó điều này

    Ví dụ:

    #Create a variable for the file name
    filename = 'Plates_output_simple.csv' #This is simply a string of text
    
    #Open the file
    infile = open(filename, 'r') # 'r' says we are opening the file to read, infile is the opened file object that we will read from
    
    #Store the data from the file in a variable
    data = infile.read()
    
    #Print the data in the file
    print(data)
    
    #close the file
    infile.close()
    
    59 sẽ yêu cầu Python xử lý chuỗi văn bản '5. 0’ là số 5. 0.
    #Create a variable for the file name
    filename = 'Plates_output_simple.csv' #This is simply a string of text
    
    #Open the file
    infile = open(filename, 'r') # 'r' says we are opening the file to read, infile is the opened file object that we will read from
    
    #Store the data from the file in a variable
    data = infile.read()
    
    #Print the data in the file
    print(data)
    
    #close the file
    infile.close()
    
    80 sẽ yêu cầu mã của chúng tôi xử lý chuỗi văn bản được lưu trữ ở vị trí thứ 5 của danh sách sline dưới dạng số nguyên (không phải số thập phân)

    Đối với mỗi dòng trong tệp, ColonyCount được lưu trữ trong cột thứ 5 (chỉ mục 4 với cách đếm dựa trên 0 của chúng tôi).
    Sửa mã ở trên để chỉ in dòng nếu ColonyCount lớn hơn 30.

    Dung dịch

    #Create a variable for the file name
    filename = 'Plates_output_simple.csv'
    
    #Open the file
    infile = open(filename, 'r') 
    
    #Print the first two lines of the file
    print(infile.readline())
    print(infile.readline())
    
    #call infile.read()
    print(infile.read())
    
    #close the file
    infile.close()
    
    3

    Ghi dữ liệu ra tệp

    Thông thường, chúng tôi sẽ muốn ghi dữ liệu vào một tệp mới. Điều này đặc biệt hữu ích nếu chúng tôi đã thực hiện nhiều tính toán hoặc xử lý dữ liệu và chúng tôi muốn có thể lưu nó và quay lại sau

    Viết một tệp là quá trình gồm nhiều bước giống nhau

    Giống như đọc một tệp, chúng tôi sẽ mở và ghi tệp theo nhiều bước

    1. Tạo một biến để giữ tên của tệp mà chúng tôi muốn mở. Thông thường, đây sẽ là một tệp mới chưa tồn tại
    2. Gọi một chức năng để mở tập tin. Lần này, chúng tôi sẽ xác định rằng chúng tôi đang mở tệp để ghi vào đó
    3. Ghi dữ liệu vào tệp. Điều này đòi hỏi một số chú ý cẩn thận để định dạng
    4. Khi chúng ta hoàn thành, chúng ta nên nhớ đóng tệp

    Đoạn mã dưới đây đưa ra một ví dụ về ghi vào một tập tin

    #Create a variable for the file name
    filename = 'Plates_output_simple.csv'
    
    #Open the file
    infile = open(filename, 'r') 
    
    #Print the first two lines of the file
    print(infile.readline())
    print(infile.readline())
    
    #call infile.read()
    print(infile.read())
    
    #close the file
    infile.close()
    
    4

    Tập tin của tôi kết thúc ở đâu?

    Bất cứ khi nào bạn mở một tệp mới và ghi vào tệp đó, tệp sẽ được lưu trong thư mục làm việc hiện tại của bạn, trừ khi bạn chỉ định một đường dẫn khác trong tên tệp biến

    Ký tự dòng mới

    Khi bạn kiểm tra tệp bạn vừa viết, bạn sẽ thấy rằng tất cả văn bản đều nằm trên cùng một dòng. Điều này là do chúng ta phải cho Python biết khi nào bắt đầu trên một dòng mới bằng cách sử dụng ký tự chuỗi đặc biệt

    #Create a variable for the file name
    filename = 'Plates_output_simple.csv' #This is simply a string of text
    
    #Open the file
    infile = open(filename, 'r') # 'r' says we are opening the file to read, infile is the opened file object that we will read from
    
    #Store the data from the file in a variable
    data = infile.read()
    
    #Print the data in the file
    print(data)
    
    #close the file
    infile.close()
    
    81. Ký tự dòng mới này sẽ cho Python biết chính xác nơi bắt đầu mỗi dòng mới

    Ví dụ dưới đây minh họa cách sử dụng các ký tự xuống dòng

    #Create a variable for the file name
    filename = 'Plates_output_simple.csv'
    
    #Open the file
    infile = open(filename, 'r') 
    
    #Print the first two lines of the file
    print(infile.readline())
    print(infile.readline())
    
    #call infile.read()
    print(infile.read())
    
    #close the file
    infile.close()
    
    6

    Mở tệp bạn vừa viết và kiểm tra xem các dòng có được đặt chính xác không

    Xử lý các ký tự xuống dòng khi bạn đọc tệp

    Bạn có thể nhận thấy trong ví dụ đọc tệp cuối cùng rằng đầu ra được in bao gồm các ký tự xuống dòng ở cuối mỗi dòng của tệp

    [‘thuộc địa02. tif', '2', 'exp', '24', '84', '3. 2’, ‘22\n’]
    [‘colonies03. tif’, ‘3’, ‘exp’, ‘24’, ‘792’, ‘3’, ‘78\n’]
    [‘colonies06. tif', '2', 'exp', '48', '85', '5. 2’, ‘46\n’]

    Chúng ta có thể loại bỏ các dòng mới này bằng cách sử dụng hàm

    #Create a variable for the file name
    filename = 'Plates_output_simple.csv' #This is simply a string of text
    
    #Open the file
    infile = open(filename, 'r') # 'r' says we are opening the file to read, infile is the opened file object that we will read from
    
    #Store the data from the file in a variable
    data = infile.read()
    
    #Print the data in the file
    print(data)
    
    #close the file
    infile.close()
    
    82, hàm này sẽ loại bỏ các ký tự xuống dòng

    #Create a variable for the file name
    filename = 'Plates_output_simple.csv'
    
    #Open the file
    infile = open(filename, 'r') 
    
    #Print the first two lines of the file
    print(infile.readline())
    print(infile.readline())
    
    #call infile.read()
    print(infile.read())
    
    #close the file
    infile.close()
    
    8

    Ghi số vào tệp

    Giống như Python tự động đọc các tệp dưới dạng chuỗi, hàm

    #Create a variable for the file name
    filename = 'Plates_output_simple.csv' #This is simply a string of text
    
    #Open the file
    infile = open(filename, 'r') # 'r' says we are opening the file to read, infile is the opened file object that we will read from
    
    #Store the data from the file in a variable
    data = infile.read()
    
    #Print the data in the file
    print(data)
    
    #close the file
    infile.close()
    
    83 dự kiến ​​chỉ ghi chuỗi. Nếu chúng ta muốn ghi các số vào một tệp, chúng ta sẽ cần "truyền" chúng thành các chuỗi bằng cách sử dụng hàm
    #Create a variable for the file name
    filename = 'Plates_output_simple.csv' #This is simply a string of text
    
    #Open the file
    infile = open(filename, 'r') # 'r' says we are opening the file to read, infile is the opened file object that we will read from
    
    #Store the data from the file in a variable
    data = infile.read()
    
    #Print the data in the file
    print(data)
    
    #close the file
    infile.close()
    
    84

    Đoạn mã dưới đây cho thấy một ví dụ về điều này

    #Create a variable for the file name
    filename = 'Plates_output_simple.csv'
    
    #Open the file
    infile = open(filename, 'r') 
    
    #Print the first two lines of the file
    print(infile.readline())
    print(infile.readline())
    
    #call infile.read()
    print(infile.read())
    
    #close the file
    infile.close()
    
    1

    Viết dòng và số mới

    Hãy mở và kiểm tra tệp bạn vừa viết. Bạn sẽ thấy rằng tất cả các số được viết trên cùng một dòng

    Sửa đổi mã để viết mỗi số trên dòng riêng của mình

    Dung dịch

    #Create a variable for the file name
    filename = 'Plates_output_simple.csv'
    
    #Open the file
    infile = open(filename, 'r') 
    
    #Print the first two lines of the file
    print(infile.readline())
    print(infile.readline())
    
    #call infile.read()
    print(infile.read())
    
    #close the file
    infile.close()
    
    2

    Tệp bạn vừa viết phải được lưu trong Thư mục làm việc của bạn. Mở tệp và kiểm tra xem đầu ra có được định dạng chính xác với một số trên mỗi dòng không

    Mở tệp ở các 'chế độ' khác nhau

    Khi mở tệp để đọc hoặc ghi dữ liệu, chúng tôi đã sử dụng tham số hàm

    #Create a variable for the file name
    filename = 'Plates_output_simple.csv' #This is simply a string of text
    
    #Open the file
    infile = open(filename, 'r') # 'r' says we are opening the file to read, infile is the opened file object that we will read from
    
    #Store the data from the file in a variable
    data = infile.read()
    
    #Print the data in the file
    print(data)
    
    #close the file
    infile.close()
    
    85 hoặc
    #Create a variable for the file name
    filename = 'Plates_output_simple.csv' #This is simply a string of text
    
    #Open the file
    infile = open(filename, 'r') # 'r' says we are opening the file to read, infile is the opened file object that we will read from
    
    #Store the data from the file in a variable
    data = infile.read()
    
    #Print the data in the file
    print(data)
    
    #close the file
    infile.close()
    
    86 để chỉ định “cách thức” để mở tệp.
    ______185 cho biết chúng tôi đang mở tệp để đọc dữ liệu từ tệp.
    ______186 cho biết chúng ta đang mở tệp để ghi dữ liệu vào đó.

    Hãy hết sức cẩn thận khi mở tệp hiện có ở chế độ 'w'.
    ______ 186 sẽ ghi đè lên bất kỳ dữ liệu nào đã có trong tệp. Dữ liệu bị ghi đè sẽ bị mất.

    Nếu bạn muốn thêm vào những gì đã có trong tệp (thay vì xóa và ghi đè lên tệp), bạn có thể mở tệp ở chế độ chắp thêm bằng cách sử dụng tham số

    #Create a variable for the file name
    filename = 'Plates_output_simple.csv'
    
    #Open the file
    infile = open(filename, 'r') 
    
    #Print the first two lines of the file
    print(infile.readline())
    print(infile.readline())
    
    #call infile.read()
    print(infile.read())
    
    #close the file
    infile.close()
    
    30 thay thế

    Kéo tất cả lại với nhau

    Đọc dữ liệu từ tệp Plates_output_simple. csv mà chúng tôi đã làm việc với. Viết một tệp có định dạng csv mới chỉ chứa các hàng dành cho biển kiểm soát.
    Bạn cần thực hiện các bước sau.

    1. Mở tập tin
    2. Sử dụng
      #Create a variable for the file name
      filename = 'Plates_output_simple.csv'
      
      #Open the file
      infile = open(filename, 'r') 
      
      #Print the first two lines of the file
      print(infile.readline())
      print(infile.readline())
      
      #call infile.read()
      print(infile.read())
      
      #close the file
      infile.close()
      
      31 để tạo danh sách các dòng trong tệp. Sau đó đóng tệp
    3. Mở một tệp để ghi đầu ra của bạn vào
    4. Viết dòng tiêu đề của tệp đầu ra
    5. Sử dụng vòng lặp for để cho phép bạn lặp qua từng dòng trong danh sách các dòng từ tệp đầu vào
    6. Đối với mỗi dòng, kiểm tra xem điều kiện tăng trưởng là thử nghiệm hay đối chứng
    7. Đối với các dòng điều khiển, ghi dòng dữ liệu vào tệp đầu ra
    8. Đóng tệp đầu ra khi bạn hoàn tất

    Dung dịch

    Đây là một cách để làm điều đó

    #Create a variable for the file name
    filename = 'Plates_output_simple.csv'
    
    #Open the file
    infile = open(filename, 'r') 
    
    #Print the first two lines of the file
    print(infile.readline())
    print(infile.readline())
    
    #call infile.read()
    print(infile.read())
    
    #close the file
    infile.close()
    
    0

    Vấn đề thử thách

    Mở và đọc dữ liệu từ Plates_output_simple. csv. Viết một tệp có định dạng csv mới chỉ chứa các hàng cho điều kiện kiểm soát và chỉ bao gồm các cột cho Thời gian, thuộc địaCount, avgColonySize và phần trămColonyArea. Gợi ý. bạn có thể dùng. hàm join() để nối danh sách các mục thành một chuỗi

    #Create a variable for the file name
    filename = 'Plates_output_simple.csv'
    
    #Open the file
    infile = open(filename, 'r') 
    
    #Print the first two lines of the file
    print(infile.readline())
    print(infile.readline())
    
    #call infile.read()
    print(infile.read())
    
    #close the file
    infile.close()
    
    1

    'Erin, Mark, Tessa'

    Dung dịch

    #Create a variable for the file name
    filename = 'Plates_output_simple.csv'
    
    #Open the file
    infile = open(filename, 'r') 
    
    #Print the first two lines of the file
    print(infile.readline())
    print(infile.readline())
    
    #call infile.read()
    print(infile.read())
    
    #close the file
    infile.close()
    
    2

    Những điểm chính

    • Mở và đọc một tệp là một quá trình gồm nhiều bước. Xác định tên tệp, mở tệp và đọc dữ liệu

    • Dữ liệu được lưu trữ trong các tệp có thể được đọc bằng nhiều lệnh khác nhau

    • Việc ghi dữ liệu vào một tệp yêu cầu chú ý đến các loại dữ liệu và định dạng không cần thiết với câu lệnh

      #Create a variable for the file name
      filename = 'Plates_output_simple.csv'
      
      #Open the file
      infile = open(filename, 'r') 
      
      #Print the first two lines of the file
      print(infile.readline())
      print(infile.readline())
      
      #call infile.read()
      print(infile.read())
      
      #close the file
      infile.close()
      
      32