Nếu bạn muốn tôi đã viết một mô -đun Python IO
giúp việc đọc tệp dễ dàng, cho phép bạn bỏ qua các bình luận, ngay cả ở giữa một dòng. Tôi đang phát triển nó trên github của tôi
data.txt
2.0 # mass
-2.0 2.0 1999 # xMin xMax nPoint
1 5 # first and last eigenvalue to print
linear # interpolation type
2 # nr. of interpolation points and xy declarations
-2.0 0.0
2.0 0.0
Mã Python chỉ bao gồm 2 dòng
In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
Như bạn có thể thấy mô -đun thậm chí còn đưa ra cảnh báo cho bạn nếu các dòng không có cùng số phần tử [vì tôi đã viết bài này để đọc dữ liệu được lập bảng]
Đầu ra là
In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
Thật không may, điều này không hoạt động cho các chuỗi, vì vậy bạn có thể muốn chọn loại nội suy có số [tức là tuyến tính = 1, bậc hai = 2, khối = 3, v.v.]
Prerequisites:
- Chế độ truy cập & nbsp;
- Mở tệp & nbsp;
- Đóng một tập tin & nbsp;
Python cung cấp các chức năng sẵn có để tạo, viết và đọc các tệp. Có hai loại tệp có thể được xử lý trong Python, tệp văn bản thông thường và tệp nhị phân [được viết bằng ngôn ngữ nhị phân, 0S và 1S]. Trong bài viết này, chúng tôi sẽ nghiên cứu đọc từng dòng từ một tệp.
Phương pháp 1: Đọc từng dòng tệp bằng cách sử dụng readlines []
Readlines [] được sử dụng để đọc tất cả các dòng trong một lần và sau đó trả về chúng dưới dạng mỗi dòng một phần tử chuỗi trong một danh sách. Hàm này có thể được sử dụng cho các tệp nhỏ, vì nó đọc toàn bộ nội dung tệp vào bộ nhớ, sau đó chia nó thành các dòng riêng biệt. Chúng ta có thể lặp lại danh sách và dải ký tự mới \ n, bằng cách sử dụng hàm Dải [].
Example:
Python3
____10
In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
1 In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
2In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
3In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
4In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
5In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
4In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
3In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
8In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
9In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
1 In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
1In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
2223In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
4In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
5In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
6In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
7In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
8In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
9In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
1 In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
1In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
2223In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
4Line1: Geeks Line2: for Line3: Geeks5
In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
6Line1: Geeks Line2: for Line3: Geeks7
In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
1 Line1: Geeks Line2: for Line3: Geeks9
Line1 Geeks Line2 for Line3 Geeks0
In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
1 Line1 Geeks Line2 for Line3 Geeks2
Line1 Geeks Line2 for Line3 Geeks3
Line1 Geeks Line2 for Line3 Geeks4
Line1 Geeks Line2 for Line3 Geeks5
Line1 Geeks Line2 for Line3 Geeks6
Line1 Geeks Line2 for Line3 Geeks7
Line1 Geeks Line2 for Line3 Geeks0
Line1 Geeks Line2 for Line3 Geeks9
In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
1 Using for loop Line1: Geeks Line2: for Line3: Geeks1
Line1 Geeks Line2 for Line3 Geeks7
Using for loop Line1: Geeks Line2: for Line3: Geeks3
In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
2Using for loop Line1: Geeks Line2: for Line3: Geeks5
Using for loop Line1: Geeks Line2: for Line3: Geeks6
Using for loop Line1: Geeks Line2: for Line3: Geeks7
Using for loop Line1: Geeks Line2: for Line3: Geeks8
Output:
Line1: Geeks Line2: for Line3: Geeks
Phương pháp 2: Đọc từng dòng tệp bằng cách sử dụng readline []
hàm readline [] đọc một dòng của tệp và trả lại dưới dạng chuỗi. Nó có một tham số n, trong đó chỉ định số lượng byte tối đa sẽ được đọc. Tuy nhiên, không đọc nhiều hơn một dòng, ngay cả khi N vượt quá độ dài của dòng. Nó sẽ hiệu quả khi đọc một tệp lớn vì thay vì tìm nạp tất cả dữ liệu trong một lần, nó tìm thấy từng dòng. readline [] trả về dòng tiếp theo của tệp có chứa ký tự dòng mới cuối cùng. Ngoài ra, nếu kết thúc của tệp, nó sẽ trả về một chuỗi trống.
Example:
Python3
____10
In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
1 In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
2In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
3In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
4In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
5In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
4In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
3In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
8In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
9In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
1 In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
1In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
2223In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
4In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
5In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
6Using readlines[] Line1: Geeks Line2: for Line3: Geeks Using readline[] Line1: Geeks Line2: for Line3: Geeks Using for loop Line1: Geeks Line2: for Line3: Geeks6
In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
8In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
9In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
1 In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
1In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
2223In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
4Line1: Geeks Line2: for Line3: Geeks5
In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
6Line1 Geeks Line2 for Line3 Geeks0
In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
1 Line1 Geeks Line2 for Line3 Geeks2
Line1 Geeks Line2 for Line3 Geeks3
Line1 Geeks Line2 for Line3 Geeks4
Line1 Geeks Line2 for Line3 Geeks5
Line1 Geeks Line2 for Line3 Geeks6
Line1 Geeks Line2 for Line3 Geeks7
Line1 Geeks Line2 for Line3 Geeks0
Line1 Geeks Line2 for Line3 Geeks9
In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
1 Using for loop Line1: Geeks Line2: for Line3: Geeks1
Phương pháp 2: Đọc từng dòng tệp bằng cách sử dụng readline []
hàm readline [] đọc một dòng của tệp và trả lại dưới dạng chuỗi. Nó có một tham số n, trong đó chỉ định số lượng byte tối đa sẽ được đọc. Tuy nhiên, không đọc nhiều hơn một dòng, ngay cả khi N vượt quá độ dài của dòng. Nó sẽ hiệu quả khi đọc một tệp lớn vì thay vì tìm nạp tất cả dữ liệu trong một lần, nó tìm thấy từng dòng. readline [] trả về dòng tiếp theo của tệp có chứa ký tự dòng mới cuối cùng. Ngoài ra, nếu kết thúc của tệp, nó sẽ trả về một chuỗi trống.
In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
05In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
06IO
9 data.txt
0data.txt
1
In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
8Output:
Line1 Geeks Line2 for Line3 Geeks
Line1 Geeks
Line2 for
Line3 Geeks
7Line1 Geeks
Line2 for
Line3 Geeks
4In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
1 In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
00
In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
Line1 Geeks Line2 for Line3 Geeks7
In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
02 In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
03 In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
04Example:
Python3
____10
In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
1 In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
2In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
3In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
4In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
5In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
4In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
3In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
8In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
9In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
1 In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
1In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
2223In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
4In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
5In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
6In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
7In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
8In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
9In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
1 In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
1In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
2223In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
4Line1: Geeks Line2: for Line3: Geeks5
In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
6Line1 Geeks Line2 for Line3 Geeks0
In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
1 Line1 Geeks Line2 for Line3 Geeks2
Line1 Geeks Line2 for Line3 Geeks3
Line1 Geeks Line2 for Line3 Geeks4
Line1 Geeks Line2 for Line3 Geeks5
Line1 Geeks Line2 for Line3 Geeks6
Line1 Geeks Line2 for Line3 Geeks7
Line1 Geeks Line2 for Line3 Geeks0
Line1 Geeks Line2 for Line3 Geeks9
In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
1 Using for loop Line1: Geeks Line2: for Line3: Geeks1
Line1 Geeks Line2 for Line3 Geeks7
Line1 Geeks Line2 for Line3 Geeks0
Line1 Geeks Line2 for Line3 Geeks9
In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
1 Using for loop Line1: Geeks Line2: for Line3: Geeks1
Line1 Geeks Line2 for Line3 Geeks7
Using for loop Line1: Geeks Line2: for Line3: Geeks3
In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
2Using for loop Line1: Geeks Line2: for Line3: Geeks5
Using for loop Line1: Geeks Line2: for Line3: Geeks6
Using for loop Line1: Geeks Line2: for Line3: Geeks7
Using for loop Line1: Geeks Line2: for Line3: Geeks8
In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
8Output:
Using for loop Line1: Geeks Line2: for Line3: Geeks
Phương pháp 2: Đọc từng dòng tệp bằng cách sử dụng readline []
hàm readline [] đọc một dòng của tệp và trả lại dưới dạng chuỗi. Nó có một tham số n, trong đó chỉ định số lượng byte tối đa sẽ được đọc. Tuy nhiên, không đọc nhiều hơn một dòng, ngay cả khi N vượt quá độ dài của dòng. Nó sẽ hiệu quả khi đọc một tệp lớn vì thay vì tìm nạp tất cả dữ liệu trong một lần, nó tìm thấy từng dòng. readline [] trả về dòng tiếp theo của tệp có chứa ký tự dòng mới cuối cùng. Ngoài ra, nếu kết thúc của tệp, nó sẽ trả về một chuỗi trống.
Thí dụ
Python3
In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
66In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
1In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
2In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
3In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
70Line1 Geeks Line2 for Line3 Geeks7
In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
72In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
1 In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
74Line1 Geeks Line2 for Line3 Geeks3
Line1 Geeks Line2 for Line3 Geeks4
Line1 Geeks Line2 for Line3 Geeks5
In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
78Using for loop Line1: Geeks Line2: for Line3: Geeks3
In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
80In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
66In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
1In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
2In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
3In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
70Line1 Geeks Line2 for Line3 Geeks7
In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
72In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
1 In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
89Line1 Geeks Line2 for Line3 Geeks3
Line1 Geeks Line2 for Line3 Geeks4
Line1 Geeks Line2 for Line3 Geeks5
In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
78Using for loop Line1: Geeks Line2: for Line3: Geeks3
In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
80Output:
['Geeks\n', 'For\n', 'Geeks'] ['Geeks', 'For', 'Geeks']
Với tuyên bố
Trong các cách tiếp cận trên, mỗi khi tập tin được mở, cần phải đóng một cách rõ ràng. Nếu người ta quên đóng tệp, nó có thể giới thiệu một số lỗi trong mã, tức là nhiều thay đổi trong các tệp không có hiệu lực cho đến khi tệp được đóng đúng. Để ngăn chặn điều này với tuyên bố có thể được sử dụng. Tuyên bố với Python được sử dụng trong xử lý ngoại lệ để làm cho mã sạch hơn và dễ đọc hơn nhiều. Nó đơn giản hóa việc quản lý các tài nguyên chung như luồng tệp. Quan sát ví dụ mã sau về cách sử dụng với câu lệnh làm cho mã sạch hơn. Không cần gọi tệp.close [] khi sử dụng với câu lệnh. Với tuyên bố tự đảm bảo mua lại và phát hành các tài nguyên thích hợp.
Example:
Python3
____10
In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
1 In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
2In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
3In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
4In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
5In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
4In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
3In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
8Các
Line1 Geeks Line2 for Line3 Geeks7
In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
13Line1 Geeks Line2 for Line3 Geeks0
In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
1 Line1 Geeks Line2 for Line3 Geeks2
Using for loop Line1: Geeks Line2: for Line3: Geeks3
In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
2In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
19In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
6In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
66In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
1In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
2In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
08In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
11Line1 Geeks Line2 for Line3 Geeks7
Line1: Geeks Line2: for Line3: Geeks7
In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
1 In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
29Line1 Geeks Line2 for Line3 Geeks7
Line1 Geeks Line2 for Line3 Geeks3
Line1 Geeks Line2 for Line3 Geeks4
Line1 Geeks Line2 for Line3 Geeks5
Line1 Geeks Line2 for Line3 Geeks6
In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
05Line1 Geeks Line2 for Line3 Geeks0
Line1 Geeks Line2 for Line3 Geeks9
In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
1 Using for loop Line1: Geeks Line2: for Line3: Geeks1
In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
05Using for loop Line1: Geeks Line2: for Line3: Geeks3
In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
2Using for loop Line1: Geeks Line2: for Line3: Geeks5
Using for loop Line1: Geeks Line2: for Line3: Geeks6
Using for loop Line1: Geeks Line2: for Line3: Geeks7
Using for loop Line1: Geeks Line2: for Line3: Geeks8
Line1 Geeks Line2 for Line3 Geeks0
In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
1 Line1 Geeks Line2 for Line3 Geeks2
Using for loop Line1: Geeks Line2: for Line3: Geeks3
In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
2In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
52In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
6In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
66In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
1In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
2In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
08In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
11Line1 Geeks Line2 for Line3 Geeks7
Line1: Geeks Line2: for Line3: Geeks7
In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
1 In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
29In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
05Line1 Geeks Line2 for Line3 Geeks0
Line1 Geeks Line2 for Line3 Geeks9
In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
1 Using for loop Line1: Geeks Line2: for Line3: Geeks1
Line1 Geeks Line2 for Line3 Geeks7
IO
9 data.txt
0data.txt
1In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
05Line1 Geeks Line2 for Line3 Geeks4
In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
1 In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
71In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
76In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
06In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
05Using for loop Line1: Geeks Line2: for Line3: Geeks3
In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
2Using for loop Line1: Geeks Line2: for Line3: Geeks5
Using for loop Line1: Geeks Line2: for Line3: Geeks6
Using for loop Line1: Geeks Line2: for Line3: Geeks7
Using for loop Line1: Geeks Line2: for Line3: Geeks8
Line1 Geeks Line2 for Line3 Geeks0
In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
1 Line1 Geeks Line2 for Line3 Geeks2
Using for loop Line1: Geeks Line2: for Line3: Geeks3
In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
2In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
90In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
6In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
66In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
1In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
2In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
08In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
11Line1 Geeks Line2 for Line3 Geeks7
Line1: Geeks Line2: for Line3: Geeks7
In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
1 In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
29In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
05Line1 Geeks Line2 for Line3 Geeks0
Line1 Geeks Line2 for Line3 Geeks9
In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
1 Using for loop Line1: Geeks Line2: for Line3: Geeks1
In [1]: import IO
In [2]: data = IO.readfile["data.txt"].tolist[] # Returns a numpy object instead
Warning: not all lines have the same shape
Most frequent lenght : 2 [4 counts]
Check rows : 0 1
05Using for loop Line1: Geeks Line2: for Line3: Geeks3
In [3]: data
Out[3]:
[[2.0],
[-2.0, 2.0, 1999.0],
[1.0, 5.0],
[2.0, 0.0],
[-2.0, 0.0],
[2.0, 0.0]]
2Using for loop Line1: Geeks Line2: for Line3: Geeks5
Using for loop Line1: Geeks Line2: for Line3: Geeks6
Using for loop Line1: Geeks Line2: for Line3: Geeks7
Using for loop Line1: Geeks Line2: for Line3: Geeks8
Output:
Using readlines[] Line1: Geeks Line2: for Line3: Geeks Using readline[] Line1: Geeks Line2: for Line3: Geeks Using for loop Line1: Geeks Line2: for Line3: Geeks