Hướng dẫn python read text file - python đọc tệp văn bản

Mở tệp trên máy chủ

Giả sử chúng ta có tệp sau, nằm trong cùng thư mục với Python:

Nội dung chính ShowShow

  • Mở tệp trên máy chủ
  • Nếu tệp được đặt ở một vị trí khác, bạn sẽ phải chỉ định đường dẫn tệp, như thế này:
  • Nếu tệp được đặt ở một vị trí khác, bạn sẽ phải chỉ định đường dẫn tệp, như thế này:
  • Nếu tệp được đặt ở một vị trí khác, bạn sẽ phải chỉ định đường dẫn tệp, như thế này:
  • Làm cách nào để đọc tệp .txt?
  • Làm cách nào để đọc tệp .text trong gấu trúc?
  • Làm thế nào để bạn trích xuất dữ liệu từ một tệp văn bản trong Python?
  • Làm cách nào để đọc một dòng tệp văn bản từng dòng trong Python?

demofile.txt

Xin chào! Chào mừng bạn đến với demofile.txtthis là nhằm mục đích thử nghiệm. Chúc may mắn!This file is for testing purposes.Good Luck!
This file is for testing purposes.
Good Luck!

Để mở tệp, sử dụng chức năng

File_object = open[r"File_Name","Access_Mode"]
88 tích hợp.

Hàm

File_object = open[r"File_Name","Access_Mode"]
88 trả về một đối tượng tệp, có phương thức

File_object.write[str1]
0 để đọc nội dung của tệp:

Thí dụ

f = open ["demofile.txt", "r"] in [f.Read []]print[f.read[]]
print[f.read[]]

Chạy ví dụ »

Nếu tệp được đặt ở một vị trí khác, bạn sẽ phải chỉ định đường dẫn tệp, như thế này:

Thí dụ

f = open ["demofile.txt", "r"] in [f.Read []]print[f.read[]]

Chạy ví dụ »
print[f.read[]]

Chạy ví dụ »

Nếu tệp được đặt ở một vị trí khác, bạn sẽ phải chỉ định đường dẫn tệp, như thế này:

Làm cách nào để đọc tệp .txt?

Thí dụ

f = open ["demofile.txt", "r"] in [f.Read []]print[f.read[]]

Chạy ví dụ »
print[f.read[5]]

Chạy ví dụ »

Nếu tệp được đặt ở một vị trí khác, bạn sẽ phải chỉ định đường dẫn tệp, như thế này:

Làm cách nào để đọc tệp .txt?

Thí dụ

f = open ["demofile.txt", "r"] in [f.Read []]print[f.read[]]

Chạy ví dụ »
print[f.readline[]]

Chạy ví dụ »

Nếu tệp được đặt ở một vị trí khác, bạn sẽ phải chỉ định đường dẫn tệp, như thế này:

Thí dụ

f = open ["demofile.txt", "r"] in [f.Read []]print[f.read[]]

Chạy ví dụ »
print[f.readline[]]
print[f.readline[]]

Chạy ví dụ »

Nếu tệp được đặt ở một vị trí khác, bạn sẽ phải chỉ định đường dẫn tệp, như thế này:

Thí dụ

f = open ["demofile.txt", "r"] in [f.Read []]print[f.read[]]

Chạy ví dụ »
for x in f:
  print[x]

Chạy ví dụ »

Nếu tệp được đặt ở một vị trí khác, bạn sẽ phải chỉ định đường dẫn tệp, như thế này:

Làm cách nào để đọc tệp .txt?

Thí dụ

f = open ["demofile.txt", "r"] in [f.Read []]print[f.read[]]

Chạy ví dụ »
print[f.readline[]]
f.close[]

Chạy ví dụ »

f = open ["demofile.txt", "r"] in [f.Read []] You should always close your files, in some cases, due to buffering, changes made to a file may not show until you close the file.

Chạy ví dụ »print[f.read[]]

  • Mở tệp trên một vị trí khác:In this type of file, Each line of text is terminated with a special character called EOL [End of Line], which is the new line character [‘\n’] in python by default.
  • Chạy ví dụ »print[f.read[5]]In this type of file, there is no terminator for a line, and the data is stored after converting it into machine-understandable binary language.

Chạy ví dụ »print[f.readline[]]

Chạy ví dụ »print[f.readline[]]print[f.readline[]]

Chạy ví dụ »for x in f:  print[x]File Handle in the file. File handle is like a cursor, which defines from where the data has to be read or written in the file. There are 6 access modes in python.

  1. Chạy ví dụ »print[f.readline[]] f.close[]Open text file for reading. The handle is positioned at the beginning of the file. If the file does not exists, raises the I/O error. This is also the default mode in which a file is opened.
  2. Nếu tệp được đặt ở một vị trí khác, bạn sẽ phải chỉ định đường dẫn tệp, như thế này: You should always close your files, in some cases, due to buffering, changes made to a file may not show until you close the file. Open the file for reading and writing. The handle is positioned at the beginning of the file. Raises I/O error if the file does not exist.
  3. Kết hợp các biểu thức thường xuyên .. Open the file for writing. For the existing files, the data is truncated and over-written. The handle is positioned at the beginning of the file. Creates the file if the file does not exist.
  4. Để tất cả chúng cùng nhau..In this type of file, Each line of text is terminated with a special character called EOL [End of Line], which is the new line character [‘\n’] in python by default.: Open the file for reading and writing. For an existing file, data is truncated and over-written. The handle is positioned at the beginning of the file.
  5. Chỉ nối thêm [‘A,]: Mở tệp để viết. Tệp được tạo nếu nó không tồn tại. Tay cầm được định vị ở cuối tệp. Dữ liệu được viết sẽ được chèn vào cuối, sau dữ liệu hiện có.: Open the file for writing. The file is created if it does not exist. The handle is positioned at the end of the file. The data being written will be inserted at the end, after the existing data.: Open the file for writing. The file is created if it does not exist. The handle is positioned at the end of the file. The data being written will be inserted at the end, after the existing data.
  6. Nối và đọc [‘A+,]: Mở tệp để đọc và viết. Tệp được tạo nếu nó không tồn tại. Tay cầm được định vị ở cuối tệp. Dữ liệu được viết sẽ được chèn vào cuối, sau dữ liệu hiện có.Open the file for reading and writing. The file is created if it does not exist. The handle is positioned at the end of the file. The data being written will be inserted at the end, after the existing data.Open the file for reading and writing. The file is created if it does not exist. The handle is positioned at the end of the file. The data being written will be inserted at the end, after the existing data.

Cách các tệp được tải vào bộ nhớ chính

Có hai loại bộ nhớ trong máy tính, tức là bộ nhớ chính và phụ Mỗi tệp mà bạn đã lưu hoặc bất kỳ ai được lưu đều trên bộ nhớ phụ gây ra bất kỳ dữ liệu nào trong bộ nhớ chính sẽ bị xóa khi máy tính bị tắt. Vì vậy, khi bạn cần thay đổi bất kỳ tệp văn bản nào hoặc chỉ để làm việc với chúng trong Python, bạn cần tải tệp đó vào bộ nhớ chính. Python tương tác với các tệp được tải trong bộ nhớ chính hoặc bộ nhớ chính thông qua các trình xử lý tệp của Google [đây là cách hệ điều hành của bạn cung cấp quyền truy cập vào Python để tương tác với tệp bạn đã mở bằng cách tìm kiếm tệp trong bộ nhớ của nó nếu thấy nó trả về trình xử lý tệp và sau đó Bạn có thể làm việc với tập tin].“file handlers” [ This is how your operating system gives access to python to interact with the file you opened by searching the file in its memory if found it returns a file handler and then you can work with the file ].“file handlers” [ This is how your operating system gives access to python to interact with the file you opened by searching the file in its memory if found it returns a file handler and then you can work with the file ].

Mở một tập tin

Nó được thực hiện bằng cách sử dụng hàm open []. Không có mô -đun nào được yêu cầu nhập khẩu cho chức năng này.

File_object = open[r"File_Name","Access_Mode"]

Tệp phải tồn tại trong cùng thư mục với tệp chương trình Python khác, địa chỉ đầy đủ của tệp nên được viết thay cho tên tệp. Lưu ý: R được đặt trước tên tệp để ngăn các ký tự trong chuỗi tệp được coi là ký tự đặc biệt. Ví dụ: nếu có \ temp trong địa chỉ tệp, thì \ t được coi là ký tự tab và lỗi được nêu ra của địa chỉ không hợp lệ. R làm cho chuỗi thô, nghĩa là, nó cho biết rằng chuỗi không có bất kỳ ký tự đặc biệt nào. R có thể bị bỏ qua nếu tệp nằm trong cùng một thư mục và địa chỉ không được đặt. & NBSP;r is placed before the filename to prevent the characters in the filename string to be treated as special characters. For example, if there is \temp in the file address, then \t is treated as the tab character, and an error is raised of invalid address. The r makes the string raw, that is, it tells that the string is without any special characters. The r can be ignored if the file is in the same directory and the address is not being placed. r is placed before the filename to prevent the characters in the filename string to be treated as special characters. For example, if there is \temp in the file address, then \t is treated as the tab character, and an error is raised of invalid address. The r makes the string raw, that is, it tells that the string is without any special characters. The r can be ignored if the file is in the same directory and the address is not being placed. 

Python

File_object.write[str1]
4
File_object.write[str1]
5
File_object.write[str1]
6
File_object.write[str1]
7
File_object.write[str1]
8
File_object.write[str1]
9
File_object.writelines[L] for L = [str1, str2, str3] 
0
File_object.writelines[L] for L = [str1, str2, str3] 
1
File_object.writelines[L] for L = [str1, str2, str3] 
2
File_object.write[str1]
5
File_object.write[str1]
6
File_object.writelines[L] for L = [str1, str2, str3] 
5
File_object.writelines[L] for L = [str1, str2, str3] 
6
File_object.write[str1]
9
File_object.writelines[L] for L = [str1, str2, str3] 
8
File_object.writelines[L] for L = [str1, str2, str3] 
1

Ở đây, File1 được tạo như một đối tượng cho MyFile1 và File2 làm đối tượng cho MyFile2

Đóng một tập tin

Đóng [] hàm đóng tệp và giải phóng không gian bộ nhớ thu được bởi tệp đó. Nó được sử dụng tại thời điểm tệp không còn cần thiết hoặc nếu nó được mở ở chế độ tệp khác. File_object.close [] & nbsp;

Python

File_object.write[str1]
4
File_object.write[str1]
5
File_object.write[str1]
6
File_object.write[str1]
7
File_object.write[str1]
8
File_object.write[str1]
9
File_object.writelines[L] for L = [str1, str2, str3] 
0
File_object.writelines[L] for L = [str1, str2, str3] 
1
File_object.writelines[L] for L = [str1, str2, str3] 
2
File_object.write[str1]
5
File_object.write[str1]
6
File_object.writelines[L] for L = [str1, str2, str3] 
5
File_object.writelines[L] for L = [str1, str2, str3] 
6
File_object.write[str1]
9
File_object.writelines[L] for L = [str1, str2, str3] 
8
File_object.writelines[L] for L = [str1, str2, str3] 
1

Ở đây, File1 được tạo như một đối tượng cho MyFile1 và File2 làm đối tượng cho MyFile22
File_object.write[str1]
5
File_object.write[str1]
6
File_object.writelines[L] for L = [str1, str2, str3] 
5
File_object.writelines[L] for L = [str1, str2, str3] 
6
File_object.write[str1]
9
File_object.writelines[L] for L = [str1, str2, str3] 
8
File_object.writelines[L] for L = [str1, str2, str3] 
1

Ở đây, File1 được tạo như một đối tượng cho MyFile1 và File2 làm đối tượng cho MyFile2

  1. Đóng một tập tin Inserts the string str1 in a single line in the text file.
File_object.write[str1]
  1. Đóng [] hàm đóng tệp và giải phóng không gian bộ nhớ thu được bởi tệp đó. Nó được sử dụng tại thời điểm tệp không còn cần thiết hoặc nếu nó được mở ở chế độ tệp khác. File_object.close [] & nbsp; For a list of string elements, each string is inserted in the text file.Used to insert multiple strings at a single time.
File_object.writelines[L] for L = [str1, str2, str3] 

File_object.write[str1]
4
File_object.write[str1]
5
File_object.write[str1]
6
File_object.write[str1]
7
File_object.write[str1]
8
File_object.write[str1]
9
File_object.writelines[L] for L = [str1, str2, str3] 
0
File_object.writelines[L] for L = [str1, str2, str3] 
1
File_object.write[str1]
68

File_object.write[str1]
72
File_object.write[str1]
5
File_object.write[str1]
6
File_object.writelines[L] for L = [str1, str2, str3] 
5
File_object.writelines[L] for L = [str1, str2, str3] 
6
File_object.write[str1]
9
File_object.writelines[L] for L = [str1, str2, str3] 
8
File_object.writelines[L] for L = [str1, str2, str3] 
1

  1. Đóng một tập tin Inserts the string str1 in a single line in the text file. Returns the read bytes in form of a string. Reads n bytes, if no n specified, reads the entire file.
File_object.write[str1]
6
  1. Đóng [] hàm đóng tệp và giải phóng không gian bộ nhớ thu được bởi tệp đó. Nó được sử dụng tại thời điểm tệp không còn cần thiết hoặc nếu nó được mở ở chế độ tệp khác. File_object.close [] & nbsp; For a list of string elements, each string is inserted in the text file.Used to insert multiple strings at a single time. Reads a line of the file and returns in form of a string.For specified n, reads at most n bytes. However, does not reads more than one line, even if n exceeds the length of the line.
Ghi vào một tệp
  1. Có hai cách để viết trong một tập tin. Reads all the lines and return them as each line a string element in a list.
File_object.write[str1]
9

Lưu ý: ‘\ n, được coi là một đặc tính đặc biệt của hai byte & nbsp;‘\n’ is treated as a special character of two bytes ‘\n’ is treated as a special character of two bytes 

Python3

File_object.write[str1]
4
File_object.write[str1]
5
File_object.write[str1]
6
File_object.write[str1]
7
File_object.write[str1]
83
File_object.write[str1]
9
File_object.write[str1]
85
File_object.writelines[L] for L = [str1, str2, str3] 
1
File_object.write[str1]
87
File_object.write[str1]
5
File_object.write[str1]
89
File_object.write[str1]
90__19
File_object.write[str1]
92219
File_object.write[str1]
96
File_object.write[str1]
97
File_object.writelines[L] for L = [str1, str2, str3] 
1
File_object.write[str1]
99
File_object.write[str1]
70
File_object.write[str1]
4
File_object.write[str1]
5
File_object.write[str1]
6
File_object.write[str1]
7
File_object.write[str1]
83
File_object.write[str1]
9
File_object.write[str1]
77
File_object.write[str1]
78
File_object.write[str1]
79
File_object.write[str1]
7
File_object.write[str1]
81
File_object.writelines[L] for L = [str1, str2, str3] 
1
File_object.write[str1]
79
File_object.write[str1]
84
File_object.write[str1]
79
File_object.write[str1]
86
File_object.write[str1]
87
File_object.write[str1]
88
File_object.write[str1]
78
File_object.write[str1]
79
File_object = open[r"File_Name","Access_Mode"]
881
File_object = open[r"File_Name","Access_Mode"]
882
File_object.writelines[L] for L = [str1, str2, str3] 
1
File_object.write[str1]
79
File_object = open[r"File_Name","Access_Mode"]
885
File_object.write[str1]
79
File_object.write[str1]
86
File_object.write[str1]
87
File_object.write[str1]
88
File_object.writelines[L] for L = [str1, str2, str3] 
1
File_object.write[str1]
79
File_object.write[str1]
7
File_object = open[r"File_Name","Access_Mode"]
883
File_object.write[str1]
78

File_object.write[str1]
79
File_object.write[str1]
86
File_object.write[str1]
87
File_object.write[str1]
88
File_object.writelines[L] for L = [str1, str2, str3] 
1
File_object.write[str1]
79
File_object.write[str1]
7
File_object.write[str1]
06
File_object.write[str1]
78
File_object.write[str1]
79
File_object.write[str1]
09
File_object = open[r"File_Name","Access_Mode"]
887
File_object = open[r"File_Name","Access_Mode"]
888
File_object.write[str1]
87
File_object.write[str1]
88
File_object.writelines[L] for L = [str1, str2, str3] 
1
File_object.write[str1]
79
File_object.write[str1]
7
File_object.write[str1]
17
File_object.write[str1]
78
File_object.write[str1]
79
File_object.write[str1]
20
File_object.write[str1]
79
File_object.write[str1]
86
File_object.write[str1]
68

Output:

File_object.write[str1]
7

Nối vào một tập tin

Python3

File_object.write[str1]
4
File_object.write[str1]
5
File_object.write[str1]
6
File_object.write[str1]
7
File_object.write[str1]
83
File_object.write[str1]
9
File_object.write[str1]
85
File_object.writelines[L] for L = [str1, str2, str3] 
1
File_object.write[str1]
87
File_object.write[str1]
5
File_object.write[str1]
89
File_object.write[str1]
90__19
File_object.write[str1]
92219
File_object.write[str1]
99
File_object.write[str1]
68
File_object.write[str1]
4
File_object.write[str1]
5
File_object.write[str1]
6
File_object.write[str1]
7
File_object.write[str1]
83
File_object.write[str1]
9
File_object.write[str1]
77
File_object.write[str1]
78
File_object.write[str1]
96
File_object.write[str1]
52
File_object.writelines[L] for L = [str1, str2, str3] 
1
File_object.write[str1]
68

Nối vào một tập tin

Nối vào một tập tin

Output:

File_object.write[str1]
8

File_object.write[str1]
4
File_object.write[str1]
5
File_object.write[str1]
6
File_object.write[str1]
7
File_object.write[str1]
83
File_object.write[str1]
9
File_object.write[str1]
85
File_object.writelines[L] for L = [str1, str2, str3] 
1
File_object.write[str1]
87
File_object.write[str1]
5
File_object.write[str1]
89
File_object.write[str1]
90__19
File_object.write[str1]
92219
File_object.write[str1]
99
File_object.write[str1]
68
File_object.write[str1]
4
File_object.write[str1]
5
File_object.write[str1]
6
File_object.write[str1]
7
File_object.write[str1]
83
File_object.write[str1]
9
File_object.write[str1]
77
File_object.write[str1]
78
File_object.write[str1]
96
File_object.write[str1]
52
File_object.writelines[L] for L = [str1, str2, str3] 
1
File_object.write[str1]
68
File Objects in Python 

File_object.write[str1]
79
File_object.write[str1]
7
File_object.write[str1]
65
File_object.write[str1]
78
File_object.write[str1]
79
File_object.write[str1]
68
File_object.write[str1]
79
File_object.write[str1]
86
File_object.write[str1]
68
File_object.write[str1]
4
File_object.write[str1]
5
File_object.write[str1]
6
File_object.write[str1]
7
File_object.write[str1]
83
File_object.write[str1]
9
File_object.write[str1]
85
File_object.writelines[L] for L = [str1, str2, str3] 
1
File_object.write[str1]
87
File_object.write[str1]
5
File_object.write[str1]
89
File_object.write[str1]
90__19
File_object.write[str1]
92219
File_object.write[str1]
68Harshit Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.
File_object.write[str1]
4
File_object.write[str1]
5
File_object.write[str1]
6
File_object.write[str1]
7
File_object.write[str1]
83
File_object.write[str1]
9
File_object.write[str1]
61
File_object.writelines[L] for L = [str1, str2, str3] 
1

Làm cách nào để đọc tệp .txt?

File_object.write[str1]
79
File_object.write[str1]
7
File_object.write[str1]
94
File_object.write[str1]
7 8
File_object.write[str1]
79
File_object.write[str1]
68
File_object.write[str1]
79
File_object.write[str1]
86
File_object.write[str1]
68with any text editor and most popular web browsers. In Windows, you can open a TXT file with Microsoft Notepad or Microsoft WordPad, both of which come included with Windows. To open a TXT file with Notepad, select File → Open....

Nối vào một tập tinFile Objects in Python 

File_object.write[str1]
4
File_object.write[str1]
5
File_object.write[str1]
6
File_object.write[str1]
7
File_object.write[str1]
83
File_object.write[str1]
9
File_object.writelines[L] for L = [str1, str2, str3] 
0
File_object.writelines[L] for L = [str1, str2, str3] 
1Harshit Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.
File_object.write[str1]
4
File_object.write[str1]
5
File_object.write[str1]
6
File_object.write[str1]
7
File_object.write[str1]
83
File_object.write[str1]
9
File_object.write[str1]
61
File_object.writelines[L] for L = [str1, str2, str3] 
1Làm cách nào để đọc tệp .txt?by using the pandas read_fwf[] function, fwf stands for fixed-width lines, you can use this to read fixed length or variable length text files. Alternatively, you can also read txt file with pandas read_csv[] function.

Bạn có thể mở một tệp TXT với bất kỳ trình soạn thảo văn bản và các trình duyệt web phổ biến nhất. Trong Windows, bạn có thể mở một tệp TXT với Microsoft Notepad hoặc Microsoft WordPad, cả hai đều đi kèm với Windows. Để mở tệp TXT bằng notepad, chọn Tệp → Mở ....with any text editor and most popular web browsers. In Windows, you can open a TXT file with Microsoft Notepad or Microsoft WordPad, both of which come included with Windows. To open a TXT file with Notepad, select File → Open....

Làm cách nào để đọc tệp .text trong gấu trúc?.

Người ta có thể đọc tệp văn bản [TXT] bằng cách sử dụng hàm gandas read_fwf [], FWF là viết tắt của các dòng chiều rộng cố định, bạn có thể sử dụng nó để đọc các tệp văn bản có độ dài hoặc độ dài thay đổi cố định.Ngoài ra, bạn cũng có thể đọc tệp TXT với hàm pandas read_csv [].by using the pandas read_fwf[] function, fwf stands for fixed-width lines, you can use this to read fixed length or variable length text files. Alternatively, you can also read txt file with pandas read_csv[] function.

Làm thế nào để bạn trích xuất dữ liệu từ một tệp văn bản trong Python?

Cách trích xuất các phần cụ thể của tệp văn bản bằng Python..

Hãy chắc chắn rằng bạn đang sử dụng Python 3 ..

Đọc dữ liệu từ một tệp văn bản ..

Sử dụng "với mở".

Đọc các tệp văn bản theo từng dòng ..

Lưu trữ dữ liệu văn bản trong một biến ..

Tìm kiếm văn bản cho một chuỗi con ..

Kết hợp các biểu thức thường xuyên ..using readlines[] readlines[] is used to read all the lines at a single go and then return them as each line a string element in a list. This function can be used for small files, as it reads the whole file content to the memory, then split it into separate lines.

Bài Viết Liên Quan

Chủ Đề