convert text to csv in python

How to Convert Text to CSV File in Python

Text files and CSV files are two most common file formats for exchanging information in today’s world. Some applications work with Text files while some work with CSV files. Sometimes you have received a text file whereas your applications accept only CSV files. In such cases, you may need to convert text files into CSV files to be able to use them with your scripts & applications. In this article, we will learn how to convert text files to CSV files using Python pandas. Basically, we will import the text file to create a Pandas dataframe. This will create a dataframe with number of rows equal to number of lines in text file and number of columns equal to number of fields in text file. Then we will call to_csv() function to export the dataframe as CSV file.


How to Convert Text to CSV File in Python

Let us look at a few examples to convert text file into CSV in python.

In the first example, we import a text file and convert it into CSV file as it is.

# import panda library
import pandas as pd

# read given csv file & create dataframe
dataframe1 = pd.read_csv("data.txt")

# storing this dataframe in a csv file
dataframe1.to_csv('data.csv', index = None)

In the above code, we import Python pandas library. Then we use read_csv() file to read a text file data.txt. It returns a pandas dataframe stored in dataframe1. Then we call to_csv() function to export it into CSV file data.csv.

The above code works when your text file’s 1st row contains column headers.

If your input data file data.txt looks like

ID Name Marks
1  John 100
2  Jim  90
3  Jane 100

Then your output file will look like.

ID,Name,Marks
1,John,100
2,Jim,90
3,Jane,100

If your data file does not contain field headers, you need to specify header=None in read_csv() function. Here is an example.

# import pandas library
import pandas as pd

# read given csv file & create dataframe
df = pd.read_csv("data.txt",header = None)

# add column headings
df.columns = ['ID', 'Name', 'Marks']

# store dataframe into csv file
df.to_csv('data.csv', index = None)

In the above code, we import Python pandas. Then we call read_csv() function with argument header=None to import text file into python dataframe. Please note, since we have specified that the input text file does not have column headers, our dataframe’s columns will not have any headers but plain indexes. So we also call .columns to set the column headers of the dataframe. Column names are important in case you want to refer to one or more specific columns in your dataframe.

Then we use to_csv() function to export the dataframe to CSV file.

In this case, if your input data file data.txt looks like the following

1  John 100
2  Jim  90
3  Jane 100

Then your output file will look like.

ID,Name,Marks
1,John,100
2,Jim,90
3,Jane,100

The default delimiter in text file is space/tab. Sometimes your text file may have a different delimiter such as ‘|’. In such cases, you need to specify the delimiter in read_csv function, using delimiter=’|’ argument.

Here is a simple example to do so.

# importing pandas library
import pandas as pd

# reading csv file & create dataframe
df = pd.read_csv("data.txt", delimiter = '|')

# store dataframe into csv file
df.to_csv('data.csv',index = None)

In the above code, we import pandas library. Then we use read_csv() file to read the text file. We specify delimiter argument to specify delimiter as ‘|’. This information is stored as pandas dataframe. We further use to_csv() function to export this dataframe to CSV file.

If your input data file data.txt looks like

ID|Name|Marks
1|John|100
2|Jim|90
3|Jane|100

Then your output file will look like.

ID,Name,Marks
1,John,100
2,Jim,90
3,Jane,100

In this article, we have learnt several ways to convert text file to CSV file in Python. You can use any of the above code in your script or application. Typically, these kind of conversions are a part of bigger function or scripts so you can customize it as per your requirement.

Please note, you need to use read_csv() function to read text file, and not read_txt() or read_text(). Also, you need to correctly specify if your input file contains header, using header argument. You also need to clearly specify the correct delimiter in your input file, using delimiter argument. The key is to correctly import your input file as a dataframe. Once that is done, you can easily export it as CSV.

Also read:

How to Split List into Even Chunks
How to Split File Into Python
How to Show File Without Comments in Linux
How to Enable Screen Sharing in Ubuntu
Linux Show Active Connections to Port

Leave a Reply

Your email address will not be published.