Python 3 Script to Convert Text File (TXT) to CSV File Using CSV Library Full Tutorial For Beginners

Python 3 Script to Convert Text File (TXT) to CSV File Using CSV Library Full Tutorial For Beginners

 

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

 

 

 

Get Started

 

 

In order to get started we need to install the following library with the help of pip command

 

pip install csv

 

After installing the library inside python project make an app.py file and copy paste the following code

 

app.py

 

 

import csv
with open('file.txt', 'r') as f:
    content = f.readlines()
    with open('test.csv', 'w+',  newline = '') as csvFile:
        csvWriter = csv.writer(csvFile, delimiter = ' ')
        for elem in content:
            csvWriter.writerow([elem.strip()])

 

 

Now when you execute this python script by running the below command

See also  Python 3 Tkinter Age Calculator From Date of Birth in Days,Months and Years GUI Script Desktop App Full Project For Beginners

 

python app.py

 

You will see the text file which is shown below will be converted to csv

 

 

 

 

 

Leave a Reply