'Only writing 2 rows from a CSV file
I am trying to write out only TWO SPECIFIC ROWS from this csv file. Here is my code
with open('StudentsMajorsList.csv', 'r') as csv_file:
csv_reader = csv.DictReader(csv_file)
with open('ComputerScienceStudents.csv', 'w') as new_file:
fieldnames = ['StudentID','Major','FirstName','LastName','DisciplinaryAction']
csv_writer = csv.DictWriter(new_file, fieldnames=fieldnames, delimiter=',')
csv_writer.writeheader()
for row in csv_reader:
csv_writer.writerow(row)
in this area
for row in csv_reader:
csv_writer.writerow(row)
I only want to pull TWO ROWS and not all of them. Please help!
Solution 1:[1]
I think you're looking for something like this:
for row in csv_reader:
if some_condition:
csv_writer.writerow(row)
You haven't told us what some_condition is, so we can only guess.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | John Gordon |
