Nếu các cột được phân tách bằng khoảng trống
import re
A,B,C,D = [],[],[],[]
pat = re.compile['[[^ ]+]\s+[[^ ]+]\s+[[^ ]+]\s+[[^ ]+]']
with open['try.txt'] as f:
for line in f:
a,b,c,d = pat.match[line.strip[]].groups[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
hoặc với mô -đun CSVcsv module
CHỈNH SỬA
A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
Nhưng nếu có nhiều hơn một khoảng trống giữa các phần tử của dữ liệu, mã này sẽ không thành công
Chỉnh sửa 2
Bởi vì giải pháp với Regex đã đủ điều kiện cực kỳ khó hiểu, nên nó có thể được xóa như sau:
import re
A,B,C,D = [],[],[],[]
pat = re.compile['\s+']
with open['try.txt'] as f:
for line in f:
a,b,c,d = pat.split[line.strip[]]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
Trong bài viết này, chúng ta sẽ xem cách chia một tệp thành một danh sách trong Python. & NBSP;
Khi chúng tôi muốn từng dòng của tệp được liệt kê tại các vị trí liên tiếp trong đó mỗi dòng trở thành một phần tử trong tệp, phương thức splutLines [] hoặc rstrip [] được sử dụng để chia tệp thành một danh sách. Hãy cùng xem một vài ví dụ để xem nó đã thực hiện như thế nào.
Ví dụ 1: Sử dụng Splitlines []splitlines[]
Tệp được mở bằng phương thức Open [] trong đó đối số đầu tiên là đường dẫn tệp và đối số thứ hai là chuỗi [chế độ] có thể là 'r', 'w', v.v. tập tin hoặc được ghi vào tệp. Ở đây khi chúng tôi đọc chế độ tệp là ‘r. Phương thức Read [] đọc dữ liệu từ tệp được lưu trữ trong biến File_Data. Phương thức splitlines [] chia dữ liệu thành các dòng và trả về một đối tượng danh sách. Sau khi in ra danh sách, tệp được đóng bằng phương thức đóng [].
Tạo một tệp văn bản có tên là Assign AssentIle.txt, như được hiển thị trong hình ảnh bên dưới được sử dụng làm đầu vào.
Python3
file_obj
=
A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
0A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
1A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
2A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
3A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
4A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
5A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
6=
A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
8A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
9=
import re
A,B,C,D = [],[],[],[]
pat = re.compile['\s+']
with open['try.txt'] as f:
for line in f:
a,b,c,d = pat.split[line.strip[]]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
1import re
A,B,C,D = [],[],[],[]
pat = re.compile['\s+']
with open['try.txt'] as f:
for line in f:
a,b,c,d = pat.split[line.strip[]]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
2import re
A,B,C,D = [],[],[],[]
pat = re.compile['\s+']
with open['try.txt'] as f:
for line in f:
a,b,c,d = pat.split[line.strip[]]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
3import re
A,B,C,D = [],[],[],[]
pat = re.compile['\s+']
with open['try.txt'] as f:
for line in f:
a,b,c,d = pat.split[line.strip[]]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
4Output:
['This is line 1,', 'This is line 2,', 'This is line 3,']
Ví dụ 2: Sử dụng rstrip []
Trong ví dụ này thay vì sử dụng phương thức splutLines [] phương thức rstrip [] được sử dụng. Phương thức rstrip [] loại bỏ các ký tự dấu vết. Nhân vật kéo dài được đưa ra trong ví dụ này là ‘\ n, đó là dòng mới. Đối với các phương thức Loop và Strip [] được sử dụng để chia tệp thành một danh sách các dòng. Các tập tin được đóng ở cuối.
Python3
file_obj
=
A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
0A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
1A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
2A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
3A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
4A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
5A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
6=
A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
8import re
A,B,C,D = [],[],[],[]
pat = re.compile['\s+']
with open['try.txt'] as f:
for line in f:
a,b,c,d = pat.split[line.strip[]]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
2import re
A,B,C,D = [],[],[],[]
pat = re.compile['\s+']
with open['try.txt'] as f:
for line in f:
a,b,c,d = pat.split[line.strip[]]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
3import re
A,B,C,D = [],[],[],[]
pat = re.compile['\s+']
with open['try.txt'] as f:
for line in f:
a,b,c,d = pat.split[line.strip[]]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
4Output:
[['This is line 1,'], ['This is line 2,'], ['This is line 3,']]
A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
9=
import re
A,B,C,D = [],[],[],[]
pat = re.compile['\s+']
with open['try.txt'] as f:
for line in f:
a,b,c,d = pat.split[line.strip[]]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
1
A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
import re
A,B,C,D = [],[],[],[]
pat = re.compile['\s+']
with open['try.txt'] as f:
for line in f:
a,b,c,d = pat.split[line.strip[]]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
Ví dụ 2: Sử dụng rstrip []
Python3
[['This is line 1,'], ['This is line 2,'], ['This is line 3,']]5
A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
0A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
1A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
2A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
3['This', 'is', 'line', '1,'] ['This', 'is', 'line', '2,'] ['This', 'is', 'line', '3,']0
['This', 'is', 'line', '1,'] ['This', 'is', 'line', '2,'] ['This', 'is', 'line', '3,']1
['This', 'is', 'line', '1,'] ['This', 'is', 'line', '2,'] ['This', 'is', 'line', '3,']2
['This is line 1,', 'This is line 2,', 'This is line 3,']8
['This', 'is', 'line', '1,'] ['This', 'is', 'line', '2,'] ['This', 'is', 'line', '3,']4
[['This is line 1,'], ['This is line 2,'], ['This is line 3,']]0
['This', 'is', 'line', '1,'] ['This', 'is', 'line', '2,'] ['This', 'is', 'line', '3,']6
['This', 'is', 'line', '1,'] ['This', 'is', 'line', '2,'] ['This', 'is', 'line', '3,']7
['This', 'is', 'line', '1,'] ['This', 'is', 'line', '2,'] ['This', 'is', 'line', '3,']8
=
['This', 'is', 'line', '1,'] ['This', 'is', 'line', '2,'] ['This', 'is', 'line', '3,']0
['This', 'is', 'line', '1,'] ['This', 'is', 'line', '2,'] ['This', 'is', 'line', '3,']7
import re
A,B,C,D = [],[],[],[]
pat = re.compile['\s+']
with open['try.txt'] as f:
for line in f:
a,b,c,d = pat.split[line.strip[]]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
2['This', 'is', 'line', '1,'] ['This', 'is', 'line', '2,'] ['This', 'is', 'line', '3,']3
Output:
['This', 'is', 'line', '1,'] ['This', 'is', 'line', '2,'] ['This', 'is', 'line', '3,']
Ví dụ 4: Tách tệp văn bản với trình tạo
Một máy phát điện trong Python là một mẹo đặc biệt có thể được sử dụng để tạo ra một mảng. Một trình tạo, giống như một hàm, trả về một mảng một mục tại một thời điểm. Từ khóa năng suất được sử dụng bởi các máy phát điện. Khi Python gặp phải một câu lệnh năng suất, nó sẽ lưu trạng thái chức năng cho đến khi trình tạo được gọi lại sau. Từ khóa năng suất đảm bảo rằng trạng thái của chúng tôi trong khi vòng lặp được lưu giữa các lần lặp. Khi xử lý các tệp lớn, điều này có thể hữu ích
Python3
['This', 'is', 'line', '1,'] ['This', 'is', 'line', '2,'] ['This', 'is', 'line', '3,']4
['This', 'is', 'line', '1,'] ['This', 'is', 'line', '2,'] ['This', 'is', 'line', '3,']5
['This', 'is', 'line', '1,'] ['This', 'is', 'line', '2,'] ['This', 'is', 'line', '3,']2
['This', 'is', 'line', '1,'] ['This', 'is', 'line', '2,'] ['This', 'is', 'line', '3,']7
=
A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
0______70['This', 'is', 'line', '1,'] ['This', 'is', 'line', '2,'] ['This', 'is', 'line', '3,']0
A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
5['This', 'is', 'line', '1,'] ['This', 'is', 'line', '2,'] ['This', 'is', 'line', '3,']2
['This is line 1,', 'This is line 2,', 'This is line 3,'] ['This', 'is', 'line', '1,'] ['This', 'is', 'line', '2,'] ['This', 'is', 'line', '3,']4
['This is line 1,', 'This is line 2,', 'This is line 3,'] ['This', 'is', 'line', '1,'] ['This', 'is', 'line', '2,'] ['This', 'is', 'line', '3,']5
['This is line 1,', 'This is line 2,', 'This is line 3,'] ['This', 'is', 'line', '1,'] ['This', 'is', 'line', '2,'] ['This', 'is', 'line', '3,']6
['This', 'is', 'line', '1,'] ['This', 'is', 'line', '2,'] ['This', 'is', 'line', '3,']7
['This', 'is', 'line', '1,'] ['This', 'is', 'line', '2,'] ['This', 'is', 'line', '3,']4
=
['This', 'is', 'line', '1,'] ['This', 'is', 'line', '2,'] ['This', 'is', 'line', '3,']7
file_obj
1['This', 'is', 'line', '1,'] ['This', 'is', 'line', '2,'] ['This', 'is', 'line', '3,']7
file_obj
3 file_obj
4 file_obj
5file_obj
6
['This', 'is', 'line', '1,'] ['This', 'is', 'line', '2,'] ['This', 'is', 'line', '3,']7
file_obj
8file_obj
6=
0
['This', 'is', 'line', '1,'] ['This', 'is', 'line', '2,'] ['This', 'is', 'line', '3,']7
=
2 =
3['This', 'is', 'line', '1,'] ['This', 'is', 'line', '2,'] ['This', 'is', 'line', '3,']8
=
=
6__12['This is line 1,', 'This is line 2,', 'This is line 3,']8
['This', 'is', 'line', '1,'] ['This', 'is', 'line', '2,'] ['This', 'is', 'line', '3,']4
[['This is line 1,'], ['This is line 2,'], ['This is line 3,']]0
A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
02['This', 'is', 'line', '1,'] ['This', 'is', 'line', '2,'] ['This', 'is', 'line', '3,']2
import re
A,B,C,D = [],[],[],[]
pat = re.compile['\s+']
with open['try.txt'] as f:
for line in f:
a,b,c,d = pat.split[line.strip[]]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
2A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
05Output:
['This', 'is', 'line', '1,'] ['This', 'is', 'line', '2,'] ['This', 'is', 'line', '3,']
Ví dụ 5: Sử dụng danh sách hiểu
Danh sách Python Hiểu là một cách tuyệt vời để làm việc với các danh sách. Danh sách toàn diện là mạnh mẽ và chúng có cú pháp ngắn hơn. & nbsp; Hơn nữa, các câu lệnh hiểu danh sách thường dễ đọc hơn.
Để đọc các tệp văn bản trong các ví dụ trước, chúng tôi đã phải sử dụng một vòng lặp. Sử dụng khả năng hiểu danh sách, chúng tôi có thể thay thế cho Loop của chúng tôi bằng một dòng mã duy nhất. Sau khi lấy dữ liệu thông qua việc hiểu danh sách, & nbsp; split [] được sử dụng & nbsp; để tách các dòng và nối & nbsp; chúng vào danh sách mới. Hãy cùng xem một ví dụ để hiểu.
After obtaining the data through list comprehension, the split[] is used to separate the lines and append them to a new list.
let’s see an example to understand.
Python3
[['This is line 1,'], ['This is line 2,'], ['This is line 3,']]5
A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
0A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
1A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
2A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
3['This', 'is', 'line', '1,'] ['This', 'is', 'line', '2,'] ['This', 'is', 'line', '3,']0
A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
12['This', 'is', 'line', '1,'] ['This', 'is', 'line', '2,'] ['This', 'is', 'line', '3,']7
['This is line 1,', 'This is line 2,', 'This is line 3,'] ['This', 'is', 'line', '1,'] ['This', 'is', 'line', '2,'] ['This', 'is', 'line', '3,']6
['This', 'is', 'line', '1,'] ['This', 'is', 'line', '2,'] ['This', 'is', 'line', '3,']2
['This', 'is', 'line', '1,'] ['This', 'is', 'line', '2,'] ['This', 'is', 'line', '3,']8
=
A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
18['This is line 1,', 'This is line 2,', 'This is line 3,']8
['This', 'is', 'line', '1,'] ['This', 'is', 'line', '2,'] ['This', 'is', 'line', '3,']4
[['This is line 1,'], ['This is line 2,'], ['This is line 3,']]0
import re
A,B,C,D = [],[],[],[]
pat = re.compile['\s+']
with open['try.txt'] as f:
for line in f:
a,b,c,d = pat.split[line.strip[]]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
2['This', 'is', 'line', '1,'] ['This', 'is', 'line', '2,'] ['This', 'is', 'line', '3,']3
['This is line 1,', 'This is line 2,', 'This is line 3,']8
['This', 'is', 'line', '1,'] ['This', 'is', 'line', '2,'] ['This', 'is', 'line', '3,']4
[['This is line 1,'], ['This is line 2,'], ['This is line 3,']]0
A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
02['This', 'is', 'line', '1,'] ['This', 'is', 'line', '2,'] ['This', 'is', 'line', '3,']2
import re
A,B,C,D = [],[],[],[]
pat = re.compile['\s+']
with open['try.txt'] as f:
for line in f:
a,b,c,d = pat.split[line.strip[]]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
2A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
05Output:
['This is line 1,', 'This is line 2,', 'This is line 3,'] ['This', 'is', 'line', '1,'] ['This', 'is', 'line', '2,'] ['This', 'is', 'line', '3,']
Ví dụ 5: Sử dụng danh sách hiểu
Danh sách Python Hiểu là một cách tuyệt vời để làm việc với các danh sách. Danh sách toàn diện là mạnh mẽ và chúng có cú pháp ngắn hơn. & nbsp; Hơn nữa, các câu lệnh hiểu danh sách thường dễ đọc hơn.
Để đọc các tệp văn bản trong các ví dụ trước, chúng tôi đã phải sử dụng một vòng lặp. Sử dụng khả năng hiểu danh sách, chúng tôi có thể thay thế cho Loop của chúng tôi bằng một dòng mã duy nhất. Sau khi lấy dữ liệu thông qua việc hiểu danh sách, & nbsp; split [] được sử dụng & nbsp; để tách các dòng và nối & nbsp; chúng vào danh sách mới. Hãy cùng xem một ví dụ để hiểu.
['This', 'is', 'line', '1,'] ['This', 'is', 'line', '2,'] ['This', 'is', 'line', '3,']2
['This', 'is', 'line', '1,'] ['This', 'is', 'line', '2,'] ['This', 'is', 'line', '3,']8
=
A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
18['This is line 1,', 'This is line 2,', 'This is line 3,']8
['This', 'is', 'line', '1,'] ['This', 'is', 'line', '2,'] ['This', 'is', 'line', '3,']4
[['This is line 1,'], ['This is line 2,'], ['This is line 3,']]0
Python3
Ví dụ 6: Chia một tệp văn bản duy nhất thành nhiều tệp văn bản
Nếu chúng tôi có một tệp lớn và xem tất cả dữ liệu trong một tệp là khó khăn, chúng tôi có thể chia dữ liệu thành nhiều tệp. Hãy để xem một ví dụ trong đó chúng tôi chia dữ liệu tệp thành hai tệp.
[['This is line 1,'], ['This is line 2,'], ['This is line 3,']]5
A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
0A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
1A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
50A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
3A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
52A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
53Cắt danh sách Python có thể được sử dụng để phân chia danh sách. Để bắt đầu, chúng tôi đọc tệp với phương thức readlines []. Tệp nửa đầu/nửa trên sau đó được sao chép vào một tệp mới có tên là nửa đầu.txt. Trong vòng này, chúng tôi sẽ sử dụng Danh sách cắt để viết nửa đầu của tệp chính & NBSP;
['This', 'is', 'line', '1,'] ['This', 'is', 'line', '2,'] ['This', 'is', 'line', '3,']7
A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
67[['This is line 1,'], ['This is line 2,'], ['This is line 3,']]5
A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
0A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
1A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
71A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
3A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
52A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
74Một vòng lặp thứ hai được sử dụng để ghi phần khác của dữ liệu vào tệp thứ hai. Nửa thứ hai của dữ liệu được chứa trong nửa sau.txt. Để thực hiện lát cắt, chúng ta cần sử dụng phương thức Len [] để xác định số lượng dòng trong tệp chính, phương thức int [] được sử dụng để chuyển đổi kết quả phân chia thành giá trị số nguyên
['This', 'is', 'line', '1,'] ['This', 'is', 'line', '2,'] ['This', 'is', 'line', '3,']7
A,B,C,D = [],[],[],[]
with open['try.txt'] as f:
for line in f:
a,b,c,d = line.split[]
A.append[int[a]];B.append[int[b]];C.append[int[c]];D.append[int[d]]
88Output: