Convert Text File to CSV using Python Pandas

Let’s see how to Convert Text File to CSV using Python Pandas. Python will read data from a text file and will create a dataframe with rows equal to number of lines present in the text file and columns equal to the number of fields present in a single line. See below example for better understanding.

Original Text File

Dataframe created from upper text file will look as follows: 

CSV File formed from given text file

Note: The first column in dataframe is indexing which is by default when a text file is read.

Once the dataframe is created, we will store this dataframe into a CSV file format using Dataframe.to_csv() Method.

Syntax: Dataframe.to_csv(parameters)



Return: None

Let’s see examples:

Example 1:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing panda library
import pandas as pd
  
# readinag given csv file
# and creating dataframe
dataframe1 = pd.read_csv("GeeksforGeeks.txt")
  
# storing this dataframe in a csv file
dataframe1.to_csv('GeeksforGeeks.csv'
                  index = None)

chevron_right


Output:

CSV File formed from given text file

The text file read is same as above. After successful run of above code, a file named “GeeksforGeeks.csv” will be created in the same directory. 

Example 2: Suppose the column heading are not given and the text file looks like:

Text File without headers

Then while writing the code you can specify headers. 



Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas library
import pandas as pd
  
# reading given csv file 
# and creating dataframe
websites = pd.read_csv("GeeksforGeeks.txt"
                       ,header = None)
  
# adding column headings
websites.columns = ['Name', 'Type', 'Website']
  
# store dataframe into csv file
websites.to_csv('GeeksforGeeks.csv'
                index = None)

chevron_right


 Output:

CSV file with headers

We see that headers have been added successfully and file has been converted from ‘.txt’ format to ‘.csv’ format.

Example 3: In this example, the fields in the text file are separated by user defined delimiter “/”. 

‘/’ Delimited Text File

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas library
import pandas as pd
  
# reading the given csv file 
# and creating dataframe
account = pd.read_csv("GeeksforGeeks.txt",
                      delimiter = '/')
  
# store dataframe into csv file
account.to_csv('GeeksforGeeks.csv',
               index = None)

chevron_right


Output:

CSV File

While reading data we specify that data should be tokenized using specified delimiter. In this case ‘/’.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.