in ,

Convert TXT To CSV Using Python Pandas

Convert Text File To CSV using Python Pandas

In this article, you’ll learn how to convert a text (.txt) file into a CSV (comma-separated values) file using Python Pandas.

Pandas is a fast, open-source library built on top of NumPy. We’ll use it to read a text file and convert it into a CSV file.

Given below is the step-by-step guide to convert a text (.txt) file into a CSV file using Python Pandas.

Steps To Convert Text (.txt) File To CSV Using Python Pandas

Create The Original Text File With Rows And Columns

First, create the original text file with the proper rows and columns structure.

In this example, we’ll be using a text file containing the roll numbers, names, classes, and scores of a few students. The fields in the text file are separated by commas, which is the default delimiter. You can also use a different delimiter of your choice, as discussed later in this article.

The text file, that we’re gonna convert to a CSV file in this example, is shown below.

Python Pandas TXT to CSV: Original Text File

Note: The first column is used for indexing by default when a text file is read.

Convert The Text File Into A CSV File Using Python Panda’s Dataframe.to_csv() Method

The Python code snippet required to convert a text file to CSV is given below:

import pandas as pd

textdatafile = pd.read_csv("path to the txt file")

textdatafile.to_csv("new csv file name", index = None)

After running the above code snippet a new csv file will be created in the same directory with the name mentioned inside the to_csv() function.

Here’s how the outputted CSV file would look:

Python Pandas TXT to CSV: Output CSV File

Configurations

Changing The Delimiter

As mentioned earlier in this article, the read_csv() method, of the pandas library of Python, expects the fields in the text to be separated by commas (,) by default.

However, we can include the delimiter parameter in the read_csv() to set our own delimiter.

For example, in the text shown below, the fields are separated by the “~” character.

Python Pandas TXT to CSV: Original Text File With Custom Delimiter

In this case, we need to specify the delimiter in the read_csv() function as shown in the Python3 code snippet given below.

import pandas as pd

textdatafile = pd.read_csv("path to the txt file", delimiter = '~')

textdatafile.to_csv("new csv file name", index = None)

The CSV output shown below.

Python Pandas TXT to CSV: Output CSV File

Specifying Own Headers

The pandas library also allows us to specify our own headers for columns if they’re not already mentioned in the text file.

Let’s take the example shown below, where there are no headers present for the columns.

Python Pandas Convert Txt To Csv: Text File Without Column Headers

In this case, we can insert the column headings using python pandas, while converting the file from TXT to CSV.

The Python3 code snippet is given below.

import pandas as pd

textdatafile = pd.read_csv("path to the txt file", header = None)

#adding column headings
textdatafile.columns = ['Roll No.', 'Name', 'Class', 'Score']

textdatafile.to_csv("new csv file name", index = None)

When the code is executed, we get a new CSV file with all the fields and the column headings mentioned in the code. The CSV output is given below.

Python Pandas TXT to CSV: Output CSV File

I hope this article helped you learn how to convert a text file into a CSV file using the pandas library of Python.

If you have any doubts or queries, feel free to comment down below. I will try my best to help you out.

Have a great day ahead!

What do you think?

106 Points
Upvote Downvote

Written by Anirban Roy

Anirban is a full-stack developer and an SEO expert. He has 6 years of experience in web development and software engineering. With a passion for writing, he has been blogging since 2017.

Leave a Reply

Your email address will not be published. Required fields are marked *