Tôi thực sự không tìm thấy một ví dụ liên quan đến câu hỏi của tôi vì tôi không biết gấu trúc nên tôi đăng nó ở đây. Hãy cho tôi biết nếu điều này không rõ ràng hoặc đã được trả lời.
Tôi có một đầu vào CSV mà tôi nhập như thế này
def import_csv[csvfilename]:
data = []
with open[csvfilename, "r", encoding="utf-8", errors="ignore"] as scraped:
reader = csv.reader[scraped, delimiter=',']
row_index = 0
for row in reader:
if row: # avoid blank lines
row_index += 1
columns = [str[row_index], row[0], row[1], row[2]]
data.append[columns]
return data
Tôi chỉ mục các hàng với input_rows [có lẽ có một cách tốt hơn cho việc này?]
Ví dụ đầu vào:
[['1',
'[FirstValue]',
'FirstText',
'AB'],
[...]
['12',
"['LastValue']",
"LastText",
'YZ']]
Tôi đang tìm cách có được hàng cuối cùng của danh sách kiểm tra này. Có một cách đơn giản để làm điều đó mà không cần lặp lại tất cả các hàng?
Cảm ơn bạn !
Làm cách nào để đọc một hàng trong tệp CSV trong Python?
Example:
Geeks,for,geeks one,of,the, top,learning,platform
Bước 1: Để đọc các hàng trong Python, trước tiên, chúng ta cần tải tệp CSV trong một đối tượng. Vì vậy, để tải tệp CSV vào một đối tượng sử dụng phương thức Open []. Bước 2: Tạo một đối tượng đầu đọc bằng cách chuyển đối tượng tệp được tạo ở trên cho hàm đầu đọc. Bước 3: Sử dụng cho vòng lặp trên đối tượng đầu đọc để có được mỗi hàng.
Làm cách nào để đọc một hàng tệp CSV theo hàng trong Python bằng Pandas?We can create a CSV file using Notepad. In the Notepad, open a new file in which separate the values by comma and save the file with .csv extension.
2. Sử dụng Excel: Chúng ta cũng có thể tạo tệp CSV bằng Excel. Trong Excel, hãy mở một tệp mới trong đó chỉ định từng giá trị trong một ô khác nhau và lưu nó bằng CSV FileType.We can also create a CSV file using Excel. In Excel, open a new file in which specify each value in a different cell and save it with filetype CSV.
Để đọc dữ liệu Row Wise từ tệp CSV trong Python, chúng ta có thể sử dụng trình đọc và trình dictreader có trong mô-đun CSV cho phép chúng ta tìm nạp dữ liệu hàng khôn ngoan.
Sử dụng độc giả
Sử dụng đầu đọc, chúng tôi có thể lặp lại giữa các hàng của tệp CSV làm danh sách các giá trị. Nó lặp lại trên tất cả các hàng trong tệp CSV và tìm nạp dữ liệu trong mỗi hàng dưới dạng danh sách. Phương thức Reader [] có mặt trong thư viện CSV. Vì vậy, để sử dụng phương thức đầu đọc này, trước tiên chúng ta cần nhập thư viện CSV. Đối tượng đầu đọc chấp nhận một tham số duy nhất có tên FileObject [một biến giữ tệp CSV]. & NBSP;
Syntax:
csv.reader[fileobject]
Các bước để đọc tệp CSV:
Bước 1: Để đọc các hàng trong Python, trước tiên, chúng ta cần tải tệp CSV trong một đối tượng. Vì vậy, để tải tệp CSV vào một đối tượng sử dụng phương thức Open [].In order to read rows in Python, First, we need to load the CSV file in one object. So to load the csv file into an object use open[] method.
with open['filename'] as fileObjectTrong khi tải tệp bằng cách chỉ định đường dẫn cùng với tên tệp, nếu bạn gặp bất kỳ lỗi unicode nào thì hãy nối thêm r trước đường dẫn của tên tệp
with open[r'path/filename'] as fileObjectBước 2: Tạo một đối tượng đầu đọc bằng cách chuyển đối tượng tệp được tạo ở trên cho hàm đầu đọc. Create a reader object by passing the above-created file object to the reader function.
reader_obj = csv.reader[file_obj]Bước 3: Sử dụng cho vòng lặp trên đối tượng đầu đọc để có được mỗi hàng. Use for loop on reader object to get each row.
Example:
Hãy xem xét một tệp CSV có tên là Samplecsv.csv. Tệp này chứa các dữ liệu sau:
Id,Name,Rating 1,Akhil,4 2,Babu,3 3,Nikhil,5
Python3
[['1',
'[FirstValue]',
'FirstText',
'AB'],
[...]
['12',
"['LastValue']",
"LastText",
'YZ']]
2 [['1',
'[FirstValue]',
'FirstText',
'AB'],
[...]
['12',
"['LastValue']",
"LastText",
'YZ']]
3[['1',
'[FirstValue]',
'FirstText',
'AB'],
[...]
['12',
"['LastValue']",
"LastText",
'YZ']]
4[['1',
'[FirstValue]',
'FirstText',
'AB'],
[...]
['12',
"['LastValue']",
"LastText",
'YZ']]
5[['1',
'[FirstValue]',
'FirstText',
'AB'],
[...]
['12',
"['LastValue']",
"LastText",
'YZ']]
6[['1',
'[FirstValue]',
'FirstText',
'AB'],
[...]
['12',
"['LastValue']",
"LastText",
'YZ']]
7[['1',
'[FirstValue]',
'FirstText',
'AB'],
[...]
['12',
"['LastValue']",
"LastText",
'YZ']]
8[['1',
'[FirstValue]',
'FirstText',
'AB'],
[...]
['12',
"['LastValue']",
"LastText",
'YZ']]
9Geeks,for,geeks one,of,the, top,learning,platform0
Geeks,for,geeks one,of,the, top,learning,platform1
Geeks,for,geeks one,of,the, top,learning,platform2
[['1',
'[FirstValue]',
'FirstText',
'AB'],
[...]
['12',
"['LastValue']",
"LastText",
'YZ']]
9Geeks,for,geeks one,of,the, top,learning,platform4
Geeks,for,geeks one,of,the, top,learning,platform5
Geeks,for,geeks one,of,the, top,learning,platform6
Geeks,for,geeks one,of,the, top,learning,platform7
Geeks,for,geeks one,of,the, top,learning,platform8
Geeks,for,geeks one,of,the, top,learning,platform9
with open['filename'] as fileObject0
Output:
['Id', 'Name', 'Rating'] ['1', 'Akhil', '4'] ['2', 'Babu', '3'] ['3', 'Nikhil', '5']
Đọc tệp CSV mà không có tiêu đề
Mọi thứ đều ổn với ví dụ trên nhưng nếu chúng ta không muốn tên cột tìm nạp hoặc chúng ta có thể nói rằng chúng ta không muốn đọc tiêu đề của tệp, thì chúng ta sẽ sử dụng phương thức tiếp theo [] trên đối tượng tệp trước khi tạo Độc giả đối tượng để nó bỏ qua các tiêu đề.
Python3
[['1',
'[FirstValue]',
'FirstText',
'AB'],
[...]
['12',
"['LastValue']",
"LastText",
'YZ']]
2 [['1',
'[FirstValue]',
'FirstText',
'AB'],
[...]
['12',
"['LastValue']",
"LastText",
'YZ']]
3[['1',
'[FirstValue]',
'FirstText',
'AB'],
[...]
['12',
"['LastValue']",
"LastText",
'YZ']]
4[['1',
'[FirstValue]',
'FirstText',
'AB'],
[...]
['12',
"['LastValue']",
"LastText",
'YZ']]
5[['1',
'[FirstValue]',
'FirstText',
'AB'],
[...]
['12',
"['LastValue']",
"LastText",
'YZ']]
6[['1',
'[FirstValue]',
'FirstText',
'AB'],
[...]
['12',
"['LastValue']",
"LastText",
'YZ']]
7[['1',
'[FirstValue]',
'FirstText',
'AB'],
[...]
['12',
"['LastValue']",
"LastText",
'YZ']]
8[['1',
'[FirstValue]',
'FirstText',
'AB'],
[...]
['12',
"['LastValue']",
"LastText",
'YZ']]
9Geeks,for,geeks one,of,the, top,learning,platform0
Geeks,for,geeks one,of,the, top,learning,platform1
Geeks,for,geeks one,of,the, top,learning,platform2
[['1',
'[FirstValue]',
'FirstText',
'AB'],
[...]
['12',
"['LastValue']",
"LastText",
'YZ']]
9Geeks,for,geeks one,of,the, top,learning,platform0
Geeks,for,geeks one,of,the, top,learning,platform1
Geeks,for,geeks one,of,the, top,learning,platform2
[['1',
'[FirstValue]',
'FirstText',
'AB'],
[...]
['12',
"['LastValue']",
"LastText",
'YZ']]
9Geeks,for,geeks one,of,the, top,learning,platform4
Geeks,for,geeks one,of,the, top,learning,platform5
Geeks,for,geeks one,of,the, top,learning,platform6
Geeks,for,geeks one,of,the, top,learning,platform7
Geeks,for,geeks one,of,the, top,learning,platform8
Geeks,for,geeks one,of,the, top,learning,platform9
with open['filename'] as fileObject0
Output:
['1', 'Akhil', '4'] ['2', 'Babu', '3'] ['3', 'Nikhil', '5']
Geeks,for,geeks
one,of,the,
top,learning,platform
8Geeks,for,geeks
one,of,the,
top,learning,platform
9with open['filename'] as fileObject
0The above code is almost the same as the code in the above example but a slight change is we use the next function here that helps us to skip the column names while accessing data from a CSV file i.e. first row. If column names are required then we access them from the heading
object i.e. return the result of the next method.
Đọc tệp CSV mà không có tiêu đề
Mọi thứ đều ổn với ví dụ trên nhưng nếu chúng ta không muốn tên cột tìm nạp hoặc chúng ta có thể nói rằng chúng ta không muốn đọc tiêu đề của tệp, thì chúng ta sẽ sử dụng phương thức tiếp theo [] trên đối tượng tệp trước khi tạo Độc giả đối tượng để nó bỏ qua các tiêu đề.
9[['1',
'[FirstValue]',
'FirstText',
'AB'],
[...]
['12',
"['LastValue']",
"LastText",
'YZ']]
with open['filename'] as fileObject
9Geeks,for,geeks
one,of,the,
top,learning,platform
1 with open[r'path/filename'] as fileObject
1with open[r'path/filename'] as fileObject
2
csv.DictReader[fileobject]
Các bước để đọc tệp CSV:
Bước 1: Để đọc các hàng trong Python, trước tiên, chúng ta cần tải tệp CSV trong một đối tượng. Vì vậy, để tải tệp CSV vào một đối tượng sử dụng phương thức Open [].Load the CSV file using the open method in a file object.
with open['filename'] as fileObjectTrong khi tải tệp bằng cách chỉ định đường dẫn cùng với tên tệp, nếu bạn gặp bất kỳ lỗi unicode nào thì hãy nối thêm r trước đường dẫn của tên tệp Create a reader object with the help of DictReader method using fileobject.
0[['1', '[FirstValue]', 'FirstText', 'AB'], [...] ['12', "['LastValue']", "LastText", 'YZ']]
Bước 2: Tạo một đối tượng đầu đọc bằng cách chuyển đối tượng tệp được tạo ở trên cho hàm đầu đọc.
Bước 3: Sử dụng cho vòng lặp trên đối tượng đầu đọc để có được mỗi hàng. Use for loop on reader object to get each row.
Example:
Python3
[['1',
'[FirstValue]',
'FirstText',
'AB'],
[...]
['12',
"['LastValue']",
"LastText",
'YZ']]
2 [['1',
'[FirstValue]',
'FirstText',
'AB'],
[...]
['12',
"['LastValue']",
"LastText",
'YZ']]
3[['1',
'[FirstValue]',
'FirstText',
'AB'],
[...]
['12',
"['LastValue']",
"LastText",
'YZ']]
4[['1',
'[FirstValue]',
'FirstText',
'AB'],
[...]
['12',
"['LastValue']",
"LastText",
'YZ']]
5[['1',
'[FirstValue]',
'FirstText',
'AB'],
[...]
['12',
"['LastValue']",
"LastText",
'YZ']]
6[['1',
'[FirstValue]',
'FirstText',
'AB'],
[...]
['12',
"['LastValue']",
"LastText",
'YZ']]
7[['1',
'[FirstValue]',
'FirstText',
'AB'],
[...]
['12',
"['LastValue']",
"LastText",
'YZ']]
8[['1',
'[FirstValue]',
'FirstText',
'AB'],
[...]
['12',
"['LastValue']",
"LastText",
'YZ']]
9Geeks,for,geeks one,of,the, top,learning,platform0
Geeks,for,geeks one,of,the, top,learning,platform1
Geeks,for,geeks one,of,the, top,learning,platform2
[['1',
'[FirstValue]',
'FirstText',
'AB'],
[...]
['12',
"['LastValue']",
"LastText",
'YZ']]
9Geeks,for,geeks one,of,the, top,learning,platform4
Geeks,for,geeks one,of,the, top,learning,platform5
Geeks,for,geeks one,of,the, top,learning,platform6
Geeks,for,geeks one,of,the, top,learning,platform7
Geeks,for,geeks one,of,the, top,learning,platform8
Geeks,for,geeks one,of,the, top,learning,platform9
with open['filename'] as fileObject0
Output:
[['1',
'[FirstValue]',
'FirstText',
'AB'],
[...]
['12',
"['LastValue']",
"LastText",
'YZ']]
1Geeks,for,geeks
one,of,the,
top,learning,platform
8Geeks,for,geeks
one,of,the,
top,learning,platform
9with open['filename'] as fileObject
0In the code, first we loaded the CSV file named samplecsv.csv and then created a reader_object that can be iterated to fetch each
row. The returned result is in the form of Key-Value pair indicates it as a dictionary. So using DictReader we read data row by row as a Dictionary.