Convert Text File to CSV using Python (tool included)

You may use the following approach in order to convert a text file to CSV using Python:

import pandas as pd

read_file = pd.read_csv (r'Path where the Text file is stored\File name.txt')
read_file.to_csv (r'Path where the CSV will be saved\File name.csv', index=None)

And if your text file doesn’t contain the column names, then you may use the technique below to add the columns:

import pandas as pd

read_file = pd.read_csv (r'Path where the Text file is stored\File name.txt', header = None)
read_file.columns = ['first_column','second_column',...]
read_file.to_csv (r'Path where the CSV will be saved\File name.csv', index=None)

Make sure that the number of columns specified matches to the number of values on each row in the text file. Otherwise, you’ll get the error of ‘Length mismatch.’

In the next section, I’ll show you the steps to convert your text file to CSV using Python. I’ll also include the code to create the following tool to perform the conversion:

Convert Text File to CSV using Python

Steps to Convert Text File to CSV using Python

Step 1: Install the pandas package

If you haven’t already done so, install the pandas package. You may use the following command to install the pandas package under Windows:

pip install pandas

Step 2: Capture the path where your text file is stored:

Next, capture the path where the text file is stored on your computer.

For example, I stored a text file (called Product_List) under the following path: C:\Users\Ron\Desktop\Test

Folder with a file

Step 3: Convert the text file to CSV using Python

Finally, you may use the template below in order to facilitate the conversion of your text file to CSV:

import pandas as pd

read_file = pd.read_csv (r'Path where the Text file is stored\File name.txt')
read_file.to_csv (r'Path where the CSV will be saved\File name.csv', index=None)

For our example:

  • The path where the Text file is stored is: C:\Users\Ron\Desktop\Test\Product_List.txt
    • Where the file name is Product_List and the file extension is txt
  • The path where the CSV will be saved is: C:\Users\Ron\Desktop\Test\New_Products.csv
    • Where the new file name to be created is New_Products and the file extension is csv

So this is the complete code to convert the Text file to CSV for our example:

import pandas as pd

read_file = pd.read_csv (r'C:\Users\Ron\Desktop\Test\Product_List.txt')
read_file.to_csv (r'C:\Users\Ron\Desktop\Test\New_Products.csv', index=None)

Once you run the code (adjusted to your paths), you’ll get the CSV file at your specified location:

Folder with files

Tool to Convert a Text File to CSV

You may use the tkinter package in order to create a simple Graphical User Interface to perform the conversion from a text file to CSV.

Here is the complete code that you may run in Python:

import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
import pandas as pd

root= tk.Tk()

canvas1 = tk.Canvas(root, width = 300, height = 300, bg = 'lightsteelblue2', relief = 'raised')
canvas1.pack()

label1 = tk.Label(root, text='File Conversion Tool', bg = 'lightsteelblue2')
label1.config(font=('helvetica', 20))
canvas1.create_window(150, 60, window=label1)

def getTxt ():
    global read_file
    
    import_file_path = filedialog.askopenfilename()
    read_file = pd.read_csv(import_file_path)
    
browseButtonTxt = tk.Button(text="      Import Text File     ", command=getTxt, bg='green', fg='white', font=('helvetica', 12, 'bold'))
canvas1.create_window(150, 130, window=browseButtonTxt)

def convertToCsv ():
    global read_file
    
    export_file_path = filedialog.asksaveasfilename(defaultextension='.csv')
    read_file.to_csv (export_file_path, index = None)

saveAsButtonCsv = tk.Button(text='Convert Text to CSV', command=convertToCsv, bg='green', fg='white', font=('helvetica', 12, 'bold'))
canvas1.create_window(150, 180, window=saveAsButtonCsv)

def exitApplication():
    MsgBox = tk.messagebox.askquestion ('Exit Application','Are you sure you want to exit the application',icon = 'warning')
    if MsgBox == 'yes':
       root.destroy()
     
exitButton = tk.Button (root, text='       Exit Application     ',command=exitApplication, bg='brown', fg='white', font=('helvetica', 12, 'bold'))
canvas1.create_window(150, 230, window=exitButton)

root.mainloop()

Once you run the code, you’ll see the following display:

Convert Text File to CSV using Python

Click on the ‘Import Text File’ button:

Import Text file

Select your text file (e.g., Product_List) and then click on Open:

Convert Text File to CSV using Python

Next, click on the ‘Convert Text to CSV‘ button:

Convert Text File to CSV using Python

Type a new name for your CSV file (e.g., New_Products), and then press on Save:

Save file

You’ll now see the CSV file at your specified location:

How to Convert Text File to CSV using Python