0% found this document useful (0 votes)
99 views7 pages

Convert Text File To CSV Using Python (Tool Included) - Data To Fish

Convert Text File to CSV using Python (tool included) - Data to Fish

Uploaded by

rajshahieee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
0% found this document useful (0 votes)
99 views7 pages

Convert Text File To CSV Using Python (Tool Included) - Data To Fish

Convert Text File to CSV using Python (tool included) - Data to Fish

Uploaded by

rajshahieee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
You are on page 1/ 7

7/18/2020 Convert Text File to CSV using Python (tool included) - Data to Fish

Data to Fish
Home » Python » Convert Text File to CSV using Python (tool included)

Convert Text File to CSV using Python (tool included)


Python / February 16, 2020

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 =
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:

https://datatofish.com/convert-text-file-to-csv-using-python-tool-included/ 1/7
7/18/2020 Convert Text File to CSV using Python (tool included) - Data to Fish

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

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:

https://datatofish.com/convert-text-file-to-csv-using-python-tool-included/ 2/7
7/18/2020 Convert Text File to CSV using Python (tool included) - Data to Fish

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:

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:

https://datatofish.com/convert-text-file-to-csv-using-python-tool-included/ 3/7
7/18/2020 Convert Text File to CSV using Python (tool included) - Data to Fish

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 =


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='gre


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='gree


canvas1.create_window(150, 180, window=saveAsButtonCsv)

def exitApplication():
MsgBox = tk.messagebox.askquestion ('Exit Application','Are you sure you want to ex
if MsgBox == 'yes':
root.destroy()

exitButton = tk.Button (root, text=' Exit Application ',command=exitApplicat


canvas1.create_window(150, 230, window=exitButton)

https://datatofish.com/convert-text-file-to-csv-using-python-tool-included/ 4/7
7/18/2020 Convert Text File to CSV using Python (tool included) - Data to Fish

root.mainloop()

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

Click on the ‘Import Text File’ button:

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

https://datatofish.com/convert-text-file-to-csv-using-python-tool-included/ 5/7
7/18/2020 Convert Text File to CSV using Python (tool included) - Data to Fish

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

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

https://datatofish.com/convert-text-file-to-csv-using-python-tool-included/ 6/7
7/18/2020 Convert Text File to CSV using Python (tool included) - Data to Fish

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

← Previous Post Next Post →

Tutorials

Python Tutorials

R Tutorials

Julia Tutorials

Batch Scripts Tutorials

MS Access Tutorials

Excel Tutorials

Copyright © 2020 | Data to Fish

Privacy Policy

Terms of Service

All rights reserved ©

https://datatofish.com/convert-text-file-to-csv-using-python-tool-included/ 7/7

You might also like