Skip to content

WebNinjaDeveloper.com

Programming Tutorials




Menu
  • Home
  • Youtube Channel
  • Official Blog
  • Nearby Places Finder
  • Direction Route Finder
  • Distance & Time Calculator
Menu

Python 3 Script to Read & Write CSV Files Using csv Module in Command Line

Posted on January 7, 2023

 

 

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

 

 

Python
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

 

 

Python
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

 

 

Python
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.

 

 

 

Recent Posts

  • Android Java Project to Merge Multiple PDF Documents From Gallery Using iTextPDF Library
  • Android Java Project to Detect System Hardware & System CPU Info & Display inside TextView Widget
  • Android Java Project to Integrate Google OAuth2 Login & Logout System & Save User Info in SharedPreferences
  • Android Java Project to Export Raw Text to PDF Document Using iTextPDF Library
  • Android Java Project to Export Images From Gallery to PDF Document Using iTextPDF Library
  • Angular
  • Bunjs
  • C#
  • Deno
  • django
  • Electronjs
  • java
  • javascript
  • Koajs
  • kotlin
  • Laravel
  • meteorjs
  • Nestjs
  • Nextjs
  • Nodejs
  • PHP
  • Python
  • React
  • ReactNative
  • Svelte
  • Tutorials
  • Vuejs




©2023 WebNinjaDeveloper.com | Design: Newspaperly WordPress Theme