How to read the specified row and column of a csv file
To read a specific row and column of a CSV file, you can use the csv module in Python. Here's a step-by-step guide:
- Import the
csvmodule:
import csv
- Open the CSV file in read mode:
with open('your_file.csv', 'r') as file:
- Create a CSV reader object:
csv_reader = csv.reader(file)
- Skip the rows you don't need until you reach the desired row (e.g., row 3 in this example):
for _ in range(3):
next(csv_reader)
- Read the specific column (e.g., column 2) from the desired row:
desired_row = next(csv_reader)
desired_column = desired_row[1] # Index 1 corresponds to the second column
- Print or use the value of the desired column:
print(desired_column)
- Close the file:
file.close()
Make sure to replace 'your_file.csv' with the actual path or filename of your CSV file. Adjust the row and column numbers as needed
原文地址: https://www.cveoy.top/t/topic/ix0e 著作权归作者所有。请勿转载和采集!