Tệp CSV trong Python w3schools

Python cũng hỗ trợ xử lý tệp và cho phép người dùng xử lý tệp i. e. , để đọc và ghi tệp, cùng với nhiều tùy chọn xử lý tệp khác, để thao tác trên tệp. Khái niệm xử lý tệp đã trải dài trên nhiều ngôn ngữ khác, nhưng việc triển khai phức tạp hoặc dài dòng, nhưng giống như các khái niệm khác của Python, khái niệm này ở đây cũng dễ dàng và ngắn gọn. Python xử lý các tệp khác nhau dưới dạng văn bản hoặc nhị phân và điều này rất quan trọng. Mỗi dòng mã bao gồm một dãy ký tự và chúng tạo thành một tệp văn bản. Mỗi dòng của tệp được kết thúc bằng một ký tự đặc biệt, được gọi là EOL hoặc ký tự Cuối dòng như dấu phẩy {,} hoặc ký tự xuống dòng. Nó kết thúc dòng hiện tại và báo cho trình thông dịch biết một dòng mới đã bắt đầu. Hãy bắt đầu với việc đọc và ghi tệp.  

Hoạt động của hàm open()

Trước khi thực hiện bất kỳ thao tác nào trên tệp như đọc, ghi, trước hết ta phải mở tệp đó. Đối với điều này, chúng ta nên sử dụng hàm open() sẵn có của Python nhưng tại thời điểm mở, chúng ta phải chỉ định chế độ, thể hiện mục đích của tệp mở

f = open(filename, mode)

Trường hợp chế độ sau được hỗ trợ

  1. r. mở một tệp hiện có cho thao tác đọc
  2. w. mở một tệp hiện có cho thao tác ghi. Nếu tệp đã chứa một số dữ liệu thì nó sẽ bị ghi đè nhưng nếu tệp không có thì nó cũng tạo tệp
  3. a. mở một tệp hiện có để nối thêm hoạt động. Nó sẽ không ghi đè dữ liệu hiện có
  4. r+. Để đọc và ghi dữ liệu vào tệp. Dữ liệu trước đó trong tệp sẽ bị ghi đè
  5. w+. Để ghi và đọc dữ liệu. Nó sẽ ghi đè dữ liệu hiện có
  6. một +. Để nối thêm và đọc dữ liệu từ tệp. Nó sẽ không ghi đè dữ liệu hiện có

Hãy xem ví dụ dưới đây

Python3




# a file named "geek", will be opened with the reading mode.

file = open('geek.txt','r'

rstrip(): This function strips each line of a file off spaces from the right-hand side.
lstrip(): This function strips each line of a file off spaces from the left-hand side.
0

rstrip(): This function strips each line of a file off spaces from the right-hand side.
lstrip(): This function strips each line of a file off spaces from the left-hand side.
1

rstrip(): This function strips each line of a file off spaces from the right-hand side.
lstrip(): This function strips each line of a file off spaces from the left-hand side.
2
rstrip(): This function strips each line of a file off spaces from the right-hand side.
lstrip(): This function strips each line of a file off spaces from the left-hand side.
3
rstrip(): This function strips each line of a file off spaces from the right-hand side.
lstrip(): This function strips each line of a file off spaces from the left-hand side.
4 file
rstrip(): This function strips each line of a file off spaces from the right-hand side.
lstrip(): This function strips each line of a file off spaces from the left-hand side.
6

rstrip(): This function strips each line of a file off spaces from the right-hand side.
lstrip(): This function strips each line of a file off spaces from the left-hand side.
7
rstrip(): This function strips each line of a file off spaces from the right-hand side.
lstrip(): This function strips each line of a file off spaces from the left-hand side.
8
rstrip(): This function strips each line of a file off spaces from the right-hand side.
lstrip(): This function strips each line of a file off spaces from the left-hand side.
9

Lệnh open sẽ mở tệp ở chế độ đọc và vòng lặp for sẽ in từng dòng có trong tệp.  

Hoạt động của chế độ read()

Có nhiều cách để đọc một tệp trong Python. Nếu bạn cần trích xuất một chuỗi chứa tất cả các ký tự trong tệp thì chúng ta có thể sử dụng tệp. đọc(). Mã đầy đủ sẽ hoạt động như thế này.  

Python3




# a file named "geek", will be opened with the reading mode.0

file = open(# a file named "geek", will be opened with the reading mode.5,43_______7

rstrip(): This function strips each line of a file off spaces from the right-hand side.
lstrip(): This function strips each line of a file off spaces from the left-hand side.
0

rstrip(): This function strips each line of a file off spaces from the right-hand side.
lstrip(): This function strips each line of a file off spaces from the left-hand side.
8 (file44_______2

Một cách khác để đọc tệp là gọi một số ký tự nhất định như trong đoạn mã sau, trình thông dịch sẽ đọc năm ký tự đầu tiên của dữ liệu được lưu trữ và trả về dưới dạng chuỗi.  

Python3




file3

file = open(# a file named "geek", will be opened with the reading mode.5,43_______7

rstrip(): This function strips each line of a file off spaces from the right-hand side.
lstrip(): This function strips each line of a file off spaces from the left-hand side.
0

rstrip(): This function strips each line of a file off spaces from the right-hand side.
lstrip(): This function strips each line of a file off spaces from the left-hand side.
8 (file=5=6=7

Tạo tệp bằng chế độ write()

Hãy xem cách tạo tệp và cách chế độ ghi hoạt động, vì vậy để thao tác với tệp, hãy viết phần sau trong môi trường Python của bạn.  

Python3




=8

file = open('geek.txt',open5

rstrip(): This function strips each line of a file off spaces from the right-hand side.
lstrip(): This function strips each line of a file off spaces from the left-hand side.
0

fileopen8_______46_______9

rstrip(): This function strips each line of a file off spaces from the right-hand side.
lstrip(): This function strips each line of a file off spaces from the left-hand side.
0

fileopen8_______47_______3

rstrip(): This function strips each line of a file off spaces from the right-hand side.
lstrip(): This function strips each line of a file off spaces from the left-hand side.
0

file(6

Lệnh close() chấm dứt tất cả các tài nguyên đang sử dụng và giải phóng hệ thống của chương trình cụ thể này.  

Hoạt động của chế độ append()

Hãy cho chúng tôi xem chế độ chắp thêm hoạt động như thế nào.  

Python3




(7

file = open('geek.txt','geek.txt'4

rstrip(): This function strips each line of a file off spaces from the right-hand side.
lstrip(): This function strips each line of a file off spaces from the left-hand side.
0

fileopen8_______48_______8

rstrip(): This function strips each line of a file off spaces from the right-hand side.
lstrip(): This function strips each line of a file off spaces from the left-hand side.
0

file(6

Ngoài ra còn có nhiều lệnh khác trong xử lý tệp được sử dụng để xử lý các tác vụ khác nhau như.  

rstrip(): This function strips each line of a file off spaces from the right-hand side.
lstrip(): This function strips each line of a file off spaces from the left-hand side.

Nó được thiết kế để cung cấp cú pháp rõ ràng hơn và xử lý ngoại lệ khi bạn đang làm việc với mã. Điều đó giải thích tại sao nên sử dụng chúng với một tuyên bố khi áp dụng. Điều này hữu ích vì sử dụng phương pháp này, bất kỳ tệp nào được mở sẽ tự động bị đóng sau khi thực hiện xong, do đó, tự động dọn dẹp.  

Thí dụ.  

Python3




,2

,3_______46_______(# a file named "geek", will be opened with the reading mode.5,7file,9

rstrip(): This function strips each line of a file off spaces from the right-hand side.
lstrip(): This function strips each line of a file off spaces from the left-hand side.
7'r'1_______45_______ file'r'4

'r'5

Sử dụng write cùng với hàm with()

Chúng ta cũng có thể sử dụng hàm write cùng với hàm  with().  

Python3




'r'6

,3_______46_______(# a file named "geek", will be opened with the reading mode.5,1_______02

rstrip(): This function strips each line of a file off spaces from the right-hand side.
lstrip(): This function strips each line of a file off spaces from the left-hand side.
03

rstrip(): This function strips each line of a file off spaces from the right-hand side.
lstrip(): This function strips each line of a file off spaces from the left-hand side.
7
rstrip(): This function strips each line of a file off spaces from the right-hand side.
lstrip(): This function strips each line of a file off spaces from the left-hand side.
05
rstrip(): This function strips each line of a file off spaces from the right-hand side.
lstrip(): This function strips each line of a file off spaces from the left-hand side.
06
rstrip(): This function strips each line of a file off spaces from the right-hand side.
lstrip(): This function strips each line of a file off spaces from the left-hand side.
0

split() sử dụng xử lý tệp

Chúng ta cũng có thể tách dòng bằng xử lý tệp trong Python. Điều này phân chia biến khi gặp không gian. Bạn cũng có thể phân chia bằng bất kỳ ký tự nào theo ý muốn của chúng tôi. Đây là mã

Python3




rstrip(): This function strips each line of a file off spaces from the right-hand side.
lstrip(): This function strips each line of a file off spaces from the left-hand side.
08

,3_______46_______(

rstrip(): This function strips each line of a file off spaces from the right-hand side.
lstrip(): This function strips each line of a file off spaces from the left-hand side.
12_______49_______# a file named "geek", will be opened with the reading mode.7,7file
rstrip(): This function strips each line of a file off spaces from the right-hand side.
lstrip(): This function strips each line of a file off spaces from the left-hand side.
6

rstrip(): This function strips each line of a file off spaces from the right-hand side.
lstrip(): This function strips each line of a file off spaces from the left-hand side.
7'r'1_______45_______ file
rstrip(): This function strips each line of a file off spaces from the right-hand side.
lstrip(): This function strips each line of a file off spaces from the left-hand side.
22

rstrip(): This function strips each line of a file off spaces from the right-hand side.
lstrip(): This function strips each line of a file off spaces from the left-hand side.
7
rstrip(): This function strips each line of a file off spaces from the right-hand side.
lstrip(): This function strips each line of a file off spaces from the left-hand side.
2
rstrip(): This function strips each line of a file off spaces from the right-hand side.
lstrip(): This function strips each line of a file off spaces from the left-hand side.
25
rstrip(): This function strips each line of a file off spaces from the right-hand side.
lstrip(): This function strips each line of a file off spaces from the left-hand side.
4
rstrip(): This function strips each line of a file off spaces from the right-hand side.
lstrip(): This function strips each line of a file off spaces from the left-hand side.
27

rstrip(): This function strips each line of a file off spaces from the right-hand side.
lstrip(): This function strips each line of a file off spaces from the left-hand side.
28
rstrip(): This function strips each line of a file off spaces from the right-hand side.
lstrip(): This function strips each line of a file off spaces from the left-hand side.
29=
rstrip(): This function strips each line of a file off spaces from the right-hand side.
lstrip(): This function strips each line of a file off spaces from the left-hand side.
31

rstrip(): This function strips each line of a file off spaces from the right-hand side.
lstrip(): This function strips each line of a file off spaces from the left-hand side.
28
rstrip(): This function strips each line of a file off spaces from the right-hand side.
lstrip(): This function strips each line of a file off spaces from the left-hand side.
8
rstrip(): This function strips each line of a file off spaces from the right-hand side.
lstrip(): This function strips each line of a file off spaces from the left-hand side.
34

Ngoài ra còn có nhiều chức năng khác giúp thao tác với các tệp và nội dung của chúng. Người ta có thể khám phá nhiều chức năng khác trong Python Docs

Bài viết này được đóng góp bởi Chinmoy Lenka. Nếu bạn thích GeeksforGeeks và muốn đóng góp, bạn cũng có thể viết một bài báo bằng cách sử dụng write. chuyên viên máy tính. org hoặc gửi bài viết của bạn tới review-team@geeksforgeeks. tổ chức. Xem bài viết của bạn xuất hiện trên trang chính của GeeksforGeeks và trợ giúp các Geeks khác

Vui lòng viết nhận xét nếu bạn thấy bất cứ điều gì không chính xác hoặc nếu bạn muốn chia sẻ thêm thông tin về chủ đề đã thảo luận ở trên.  

Làm cách nào để sử dụng tệp CSV trong Python?

Đọc tệp CSV bằng Python .
Sử dụng Thư viện CSV. nhập csv với open(". /wq. csv", 'r') dưới dạng tệp. csvreader = csv. trình đọc (tệp) cho hàng trong csvreader. print(row) Ở đây chúng tôi đang nhập thư viện csv để sử dụng. .
Sử dụng thư viện Pandas. nhập gấu trúc dưới dạng dữ liệu pd = pd. read_csv("bwq. csv") dữ liệu

Tệp CSV trong Python là gì?

Tệp CSV (tệp Giá trị được phân tách bằng dấu phẩy) là một loại tệp văn bản thuần sử dụng cấu trúc cụ thể để sắp xếp dữ liệu dạng bảng . Bởi vì nó là một tệp văn bản thuần túy, nên nó chỉ có thể chứa dữ liệu văn bản thực—nói cách khác, các ký tự ASCII hoặc Unicode có thể in được. Cấu trúc của tệp CSV được đặt theo tên của nó.

Bạn có thể ghi trực tiếp vào tệp CSV bằng Python không?

Lớp DictWriter() có thể được sử dụng để ghi vào tệp CSV từ từ điển Python . Ở đây, tệp - tệp CSV nơi chúng tôi muốn ghi vào. tên trường - một đối tượng danh sách sẽ chứa các tiêu đề cột chỉ định thứ tự ghi dữ liệu trong tệp CSV.

Làm cách nào để tạo tệp CSV?

Mở Microsoft Excel (đảm bảo rằng đó là tệp Excel bạn muốn sao chép ở định dạng CSV). .
Bên dưới tên tệp ở cuối màn hình lưu, bạn sẽ thấy tùy chọn “Lưu dưới dạng. ” Trong trường này, hãy chọn “CSV (Được phân cách bằng dấu phẩy). ”.
Hai hộp thoại “cửa sổ bật lên” sẽ xuất hiện cảnh báo bạn rằng định dạng khác