How to write CSV file in python?
A CSV file stands for comma separated values file is a text based format represents data found in spredsheet.
Now we will see how to read CSV file in python?
There are basically two methods to read the file :-
- By importing CSV module
- By importing pandas module
In the first method we have to directly import csv module from python packages. And then we are able to read the csv file by following the given syntax:-
import csv filename="file_name.csv" csv_reader=csv.reader(filename) print(csv_reader)
In the second method we have to import pandas module from the python packages.
import pandas as pd data=pd.read_csv("path of file") print(data)
We can also read and print some specific columns and rows .
import pandas as pd data=pd.read_csv("path of file") d=pd.read_csv("path of file", column="column_name") print(d)
We can write in csv file.
import csv with open (filename, w) as csv: #all data that we have to write in file writeheader() or writerow()
This is all about how we can write in csv file in python.
Please login to comment
View all comments