Bạn có dữ liệu CSV [phân tách bằng dấu phẩy]. Dễ nhất là sử dụng mô -đun csv
:
import csv
all_values = []
with open['scores.txt', newline=''] as infile:
reader = csv.reader[infile]
for row in reader:
all_values.extend[row]
Nếu không, phân chia các giá trị. Mỗi dòng bạn đọc là một chuỗi có ký tự ','
giữa các chữ số:
all_values = []
with open['scores.txt', newline=''] as infile:
for line in infile:
all_values.extend[line.strip[].split[',']]
Dù bằng cách nào, all_values
kết thúc với một danh sách các chuỗi. Nếu tất cả các giá trị của bạn chỉ bao gồm các chữ số, bạn có thể chuyển đổi chúng thành số nguyên:
all_values.extend[map[int, row]]
hoặc
all_values.extend[map[int, line.strip[].split[',']]]
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
- Bàn luận Open the file for writing.
- Trong khi đọc hoặc ghi vào một tệp, chế độ truy cập chi phối loại hoạt động có thể trong tệp đã mở. Nó đề cập đến cách các tập tin sẽ được sử dụng sau khi nó mở. Các chế độ này cũng xác định vị trí của xử lý tệp trong tệp. Định nghĩa của các chế độ truy cập này như sau: Open the file for reading and writing.
Chỉ nối thêm [‘A,]: Mở tệp để viết.
Nối và đọc [‘A+,]: Mở tệp để đọc và viết. Python program to illustrate Append vs write mode.
Python3
Khi tệp được mở ở chế độ nối trong Python, 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ó. & NBSP;
Ví dụ 1: Chương trình Python để minh họa Chế độ ghi vs Viết.
all_values.extend[map[int, row]]
7all_values.extend[map[int, row]]
8Các
Các
all_values.extend[map[int, row]]
8Các
Output of Readlines after appending This is Delhi This is Paris This is LondonToday Output of Readlines after writing Tomorrow9
all_values = []
with open['scores.txt', newline=''] as infile:
for line in infile:
all_values.extend[line.strip[].split[',']]
3Output of Readlines after appending This is Delhi This is Paris This is London TodayTomorrow1
all_values = []
with open['scores.txt', newline=''] as infile:
for line in infile:
all_values.extend[line.strip[].split[',']]
7Output of Readlines after appending This is Delhi This is Paris This is LondonToday Output of Readlines after writing Tomorrow9
Output of Readlines after appending This is Delhi This is Paris This is London TodayTomorrow4
Output of Readlines after appending This is Delhi This is Paris This is LondonToday Output of Readlines after writing Tomorrow9
Output of Readlines after appending This is Delhi This is Paris This is London TodayTomorrow6
all_values.extend[map[int, row]]
8all_values.extend[map[int, line.strip[].split[',']]]
7all_values.extend[map[int, line.strip[].split[',']]]
8all_values = []
with open['scores.txt', newline=''] as infile:
for line in infile:
all_values.extend[line.strip[].split[',']]
7all_values.extend[map[int, line.strip[].split[',']]]
7Hello This is Delhi This is Paris This is London Today7
all_values = []
with open['scores.txt', newline=''] as infile:
for line in infile:
all_values.extend[line.strip[].split[',']]
7all_values.extend[map[int, row]]
8Các
Output of Readlines after appending This is Delhi This is Paris This is LondonToday Output of Readlines after writing Tomorrow9
all_values = []
with open['scores.txt', newline=''] as infile:
for line in infile:
all_values.extend[line.strip[].split[',']]
3','
0all_values = []
with open['scores.txt', newline=''] as infile:
for line in infile:
all_values.extend[line.strip[].split[',']]
7Output of Readlines after appending This is Delhi This is Paris This is LondonToday Output of Readlines after writing Tomorrow9
Output of Readlines after appending This is Delhi This is Paris This is London TodayTomorrow4
Output of Readlines after appending This is Delhi This is Paris This is LondonToday Output of Readlines after writing Tomorrow9
Output of Readlines after appending This is Delhi This is Paris This is London TodayTomorrow6
all_values.extend[map[int, row]]
8Output:
Output of Readlines after appending This is Delhi This is Paris This is LondonToday Output of Readlines after writing Tomorrow
all_values.extend[map[int, line.strip[].split[',']]]
7all_values.extend[map[int, line.strip[].split[',']]]
8all_values = []
with open['scores.txt', newline=''] as infile:
for line in infile:
all_values.extend[line.strip[].split[',']]
7 Append data from a new line
all_values.extend[map[int, line.strip[].split[',']]]
all_values.extend[map[int, line.strip[].split[',']]]
all_values = []
with open['scores.txt', newline=''] as infile:
for line in infile:
all_values.extend[line.strip[].split[',']]
Các
Python3
Khi tệp được mở ở chế độ nối trong Python, 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ó. & NBSP;
Ví dụ 1: Chương trình Python để minh họa Chế độ ghi vs Viết.
all_values.extend[map[int, row]]
7all_values.extend[map[int, row]]
8Các
all_values.extend[map[int, line.strip[].split[',']]]
7all_values = []
with open['scores.txt', newline=''] as infile:
for line in infile:
all_values.extend[line.strip[].split[',']]
15all_values = []
with open['scores.txt', newline=''] as infile:
for line in infile:
all_values.extend[line.strip[].split[',']]
7all_values.extend[map[int, line.strip[].split[',']]]
7all_values = []
with open['scores.txt', newline=''] as infile:
for line in infile:
all_values.extend[line.strip[].split[',']]
18all_values = []
with open['scores.txt', newline=''] as infile:
for line in infile:
all_values.extend[line.strip[].split[',']]
7all_values.extend[map[int, line.strip[].split[',']]]
7all_values = []
with open['scores.txt', newline=''] as infile:
for line in infile:
all_values.extend[line.strip[].split[',']]
21all_values = []
with open['scores.txt', newline=''] as infile:
for line in infile:
all_values.extend[line.strip[].split[',']]
7Các
Output of Readlines after appending This is Delhi This is Paris This is LondonToday Output of Readlines after writing Tomorrow9
all_values = []
with open['scores.txt', newline=''] as infile:
for line in infile:
all_values.extend[line.strip[].split[',']]
3Output of Readlines after appending This is Delhi This is Paris This is London TodayTomorrow1
all_values = []
with open['scores.txt', newline=''] as infile:
for line in infile:
all_values.extend[line.strip[].split[',']]
7Output of Readlines after appending This is Delhi This is Paris This is LondonToday Output of Readlines after writing Tomorrow9
Output of Readlines after appending This is Delhi This is Paris This is London TodayTomorrow4
Output of Readlines after appending This is Delhi This is Paris This is LondonToday Output of Readlines after writing Tomorrow9
Output of Readlines after appending This is Delhi This is Paris This is London TodayTomorrow6
all_values.extend[map[int, row]]
8Output:
Output of Readlines after appending This is Delhi This is Paris This is London TodayTomorrow
7all_values.extend[map[int, line.strip[].split[',']]]
8all_values.extend[map[int, line.strip[].split[',']]]
7 ‘\n’ is treated as a special character of two bytes.all_values = []
with open['scores.txt', newline=''] as infile:
for line in infile:
all_values.extend[line.strip[].split[',']]
Các Using With statement in Python
Các is used in exception handling to make the code cleaner and much more readable. It simplifies the management of common resources like file streams. Unlike the above implementations, there is no need to call file.close[] when using with statement. The with statement itself ensures proper acquisition and release of resources.
Python3
Ví dụ 2: & nbsp; nối dữ liệu từ một dòng mới
all_values = []
with open['scores.txt', newline=''] as infile:
for line in infile:
all_values.extend[line.strip[].split[',']]
49all_values = []
with open['scores.txt', newline=''] as infile:
for line in infile:
all_values.extend[line.strip[].split[',']]
2all_values = []
with open['scores.txt', newline=''] as infile:
for line in infile:
all_values.extend[line.strip[].split[',']]
3all_values = []
with open['scores.txt', newline=''] as infile:
for line in infile:
all_values.extend[line.strip[].split[',']]
4all_values = []
with open['scores.txt', newline=''] as infile:
for line in infile:
all_values.extend[line.strip[].split[',']]
5all_values = []
with open['scores.txt', newline=''] as infile:
for line in infile:
all_values.extend[line.strip[].split[',']]
6all_values = []
with open['scores.txt', newline=''] as infile:
for line in infile:
all_values.extend[line.strip[].split[',']]
55all_values = []
with open['scores.txt', newline=''] as infile:
for line in infile:
all_values.extend[line.strip[].split[',']]
56all_values.extend[map[int, line.strip[].split[',']]]
7all_values = []
with open['scores.txt', newline=''] as infile:
for line in infile:
all_values.extend[line.strip[].split[',']]
58all_values = []
with open['scores.txt', newline=''] as infile:
for line in infile:
all_values.extend[line.strip[].split[',']]
7all_values = []
with open['scores.txt', newline=''] as infile:
for line in infile:
all_values.extend[line.strip[].split[',']]
56all_values.extend[map[int, row]]
7all_values = []
with open['scores.txt', newline=''] as infile:
for line in infile:
all_values.extend[line.strip[].split[',']]
49all_values = []
with open['scores.txt', newline=''] as infile:
for line in infile:
all_values.extend[line.strip[].split[',']]
2all_values = []
with open['scores.txt', newline=''] as infile:
for line in infile:
all_values.extend[line.strip[].split[',']]
3all_values = []
with open['scores.txt', newline=''] as infile:
for line in infile:
all_values.extend[line.strip[].split[',']]
4all_values = []
with open['scores.txt', newline=''] as infile:
for line in infile:
all_values.extend[line.strip[].split[',']]
5all_values = []
with open['scores.txt', newline=''] as infile:
for line in infile:
all_values.extend[line.strip[].split[',']]
67all_values = []
with open['scores.txt', newline=''] as infile:
for line in infile:
all_values.extend[line.strip[].split[',']]
55all_values = []
with open['scores.txt', newline=''] as infile:
for line in infile:
all_values.extend[line.strip[].split[',']]
56all_values.extend[map[int, line.strip[].split[',']]]
7all_values = []
with open['scores.txt', newline=''] as infile:
for line in infile:
all_values.extend[line.strip[].split[',']]
18all_values = []
with open['scores.txt', newline=''] as infile:
for line in infile:
all_values.extend[line.strip[].split[',']]
7Trong ví dụ trên về xử lý tệp, có thể thấy rằng dữ liệu không được thêm vào dòng mới. Điều này có thể được thực hiện bằng cách viết ký tự mới ‘\ n, vào tệp. & Nbsp;
all_values = []
with open['scores.txt', newline=''] as infile:
for line in infile:
all_values.extend[line.strip[].split[',']]
56Output of Readlines after appending This is Delhi This is Paris This is LondonToday Output of Readlines after writing Tomorrow9
Output of Readlines after appending This is Delhi This is Paris This is London TodayTomorrow4
Output:
Hello This is Delhi This is Paris This is London Today
Các To know more about with statement click here.