Computer Science
Write a function that reads a csv file and creates another csv file with the same content, but with a different delimiter.
Answer
Let "original.csv" file contain the following data:
Product,Price,Quantity
Apple,1.99,100
Banana,0.99,150
Orange,2.49,80
import csv
def change_delimiter(input_file, output_file, input_delimiter, output_delimiter):
with open(input_file, 'r', newline='') as f_in:
reader = csv.reader(f_in, delimiter = input_delimiter)
data = list(reader)
with open(output_file, 'w', newline='') as f_out:
writer = csv.writer(f_out, delimiter = output_delimiter)
writer.writerows(data)
change_delimiter('original.csv', 'modified.csv', ',', '|')
Contents of "modified.csv":
Product|Price|Quantity
Apple|1.99|100
Banana|0.99|150
Orange|2.49|80
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 except the lines beginning with 'check'.
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.