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 Convert Text File to CSV and Save it in Local File System Using csv Module in Command Line

Posted on November 19, 2022

 

 

Welcome folks today in this blog post we will be converting the text file to csv and saving it inside the local file system using the csv module in python. All the full source code of the application is shown below.

 

 

Get Started

 

 

In order to get started you need to install the csv module using the pip command as shown below

 

 

pip install csv

 

 

After that you need to create the app.py file and copy paste the following code

 

 

app.py

 

 

Python
1
2
3
4
5
6
7
8
import csv
 
with open('file.txt') as report:
    reader = csv.reader(report, delimiter="\\")
    with open('output.csv', 'w', newline='') as csvfile:
        writer = csv.writer(csvfile)
        for line in reader:
            writer.writerow(line)

 

 

As you can see we are importing the csv module at the very top of the file and then we are opening the input text file called file.txt and then we are reading it using the open() method in python. And after that we are using the reader() method of the csv module to convert the text file to csv using the delimiter. And then we are saving the output csv file using the open() method. And then we are writing the content of the text file to the csv output file using the writer() and writerrow() method

Recent Posts

  • Android Java Project to Crop,Scale & Rotate Images Selected From Gallery and Save it inside SD Card
  • Android Kotlin Project to Load Image From URL into ImageView Widget
  • Android Java Project to Make HTTP Call to JSONPlaceholder API and Display Data in RecyclerView Using GSON & Volley Library
  • Android Java Project to Download Youtube Video Thumbnail From URL & Save it inside SD Card
  • Android Java Project to Embed Google Maps & Add Markers Using Maps SDK
  • 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