Computer Science
Write a function that reads a csv file and creates another csv file with the same content except the lines beginning with 'check'.
Python File Handling
5 Likes
Answer
Let "input.csv" file contain the following data:
check1,10,A
check2,20,B
data1,30,C
check3,40,D
data2,50,E
import csv
def filter(input_file, output_file):
with open(input_file, 'r', newline='') as f_in, open(output_file, 'w', newline='') as f_out:
reader = csv.reader(f_in)
writer = csv.writer(f_out)
for row in reader:
if not row[0].startswith('check'):
writer.writerow(row)
filter('input.csv', 'output.csv')
Contents of "output.csv":
data1,30,C
data2,50,E
Answered By
2 Likes
Related Questions
Write a Python program to read a given CSV file having tab delimiter.
Write a Python program to write a nested Python list to a csv file in one go. After writing the CSV file read the CSV file and display the content.
Write a function that reads a csv file and creates another csv file with the same content, but with a different delimiter.
Give any one point of difference between a binary file and a CSV file.
Write a Program in Python that defines and calls the following user defined functions :
(a) add(). To accept and add data of an employee to a CSV file 'furdata.csv'. Each record consists of a list with field elements as fid, fname and fprice to store furniture id, furniture name and furniture price respectively.
(b) search(). To display the records of the furniture whose price is more than 10000.