Welcome folks today in this blog post we will be reading
and writing
csv files using csv
module in command line using python. All the full source code of the application is shown below.
Get Started
In order to get started you need to make an app.py
file and copy paste the following code
app.py
Writing CSV Files
First of all we will be using the writerow()
method to write a single record
inside the csv
file. For this we will be using the for
loop to iterate all over the records which is present inside the array
of users as shown below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import csv # Open a CSV file for writing with open('output.csv', 'w', newline='') as csvfile: # Create a CSV writer object writer = csv.writer(csvfile) # Some data we want to write to the CSV file rows = [ ['Name', 'Age', 'Country'], ['Alice', '25', 'USA'], ['Bob', '30', 'Canada'], ['Claire', '35', 'UK'], ['David', '40', 'Australia'], ] # Write each row to the CSV file for row in rows: writer.writerow(row) |
As you can see we are importing the csv
module at the top and then we are using the open()
method to open the new output
file called output.csv
and then we are using the w
flag to open the file in write
mode and inside it we are using the writer()
method to initialize the writer object and inside it we are declaring the rows
array of users and we have three
columns such as name
age and country
and then we are using the for
loop to insert each row
using the writerow()
method
Now if you execute the python
script inside the command line then the output.csv
file will be created as shown below
python app.py
Adding Multiple Rows at Once
Now we can use the writerows()
method to insert multiple rows of data at once inside the csv
file as shown below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import csv # Open a CSV file for writing with open('output.csv', 'w', newline='') as csvfile: # Create a CSV writer object writer = csv.writer(csvfile) # Some data we want to write to the CSV file rows = [ ['Name', 'Age', 'Country'], ['Alice', '25', 'USA'], ['Bob', '30', 'Canada'], ['Claire', '35', 'UK'], ['David', '40', 'Australia'], ] # Write all the rows to the CSV file writer.writerows(rows) |
As you can see for this example
we no longer need to use the for
loop we can directly pass the array
of records straight to the writerows()
method to create the output.csv
file. The same output will be created as shown below
Reading CSV Files
Now we will be reading the content
of the csv files as shown below
app.py
1 2 3 4 5 6 7 8 9 |
import csv csv_file = open('output.csv', 'r') csv_reader = csv.reader(csv_file) for row in csv_reader: print(row) csv_file.close() |
As you can see we are opening the csv
file using the open()
method inside the r
mode which stands for read mode. And then we are using the reader()
method to read the csv file. And then we are using the for
loop to iterate over all the contents which are there inside the csv file. And then we are printing it using the print()
method.