from csv import readerfrom csv import DictReaderfrom csv import writerfrom csv import DictWriter# Reading csv filesreaderwith open("file.csv") as f1: csv_file = reader(f1) for data in csv_file: print(data)# DictReaderwith open("file.csv") as f2: my_csv = DictReader(f2, delimiter="#") for i in my_csv: print(i)# Writing csv files# writerwith open("My_csv.csv","w",newline="") as f3: write_csv = writer(f3)# # writerow write_csv.writerow(["first_name","last_name","age"]) write_csv.writerow(["Adrika","Bhadauria","14"]) write_csv.writerow(["Anika","Singh","3"]) # writerows write_csv.writerows([["first_name","last_name","age"], ["Adrika","Bhadauria","14"], ["Anika","Singh","3"]])# DictWriterwith open("learn_csv.csv","w",newline="") as f4: dict_write = DictWriter(f4,fieldnames=["first_name","last_name","age"]) dict_write.writeheader() # writerow dict_write.writerow({"first_name":"Adrika", "last_name":"Bhadauria", "age":14}) dict_write.writerow({"first_name":"Anika", "last_name":"Singh", "age":3}) # writerows dict_write.writerows([ {"first_name":"Adrika","last_name":"Bhadauria","age":14}, {"first_name":"Anika","last_name":"Singh","age":3} ])
Post a Comment