Read Specific CSV Line in Python
To read specific rows of a CSV file, you can use the reader function from the csv module combined with the enumerate function. Here is an example code:
import csv
filename = 'data.csv'
row_number = 3
with open(filename, 'r') as file:
csv_reader = csv.reader(file)
for i, row in enumerate(csv_reader):
if i == row_number - 1:
print(row)
break
In the above example, we opened a CSV file named “data.csv” and specified that we want to read line number 3. We then used the enumerate function to loop through the rows in csv_reader and found the specified row by comparing row numbers. Once the specified row was found, we printed out its contents and used a break statement to end the loop.