Convert Text File To CSV Using Python (Tool Included) - Data To Fish
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)
You may use the following approach in order to convert a text file to CSV using Python:
import pandas as pd
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
If you haven’t already done so, install the pandas package. You may use the following command to install the
pandas package under Windows:
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
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
So this is the complete code to convert the Text file to CSV for our example:
import pandas as pd
Once you run the code (adjusted to your paths), you’ll get the CSV file at your specified location:
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()
import_file_path = filedialog.askopenfilename()
read_file = pd.read_csv(import_file_path)
export_file_path = filedialog.asksaveasfilename(defaultextension='.csv')
read_file.to_csv (export_file_path, index = None)
def exitApplication():
MsgBox = tk.messagebox.askquestion ('Exit Application','Are you sure you want to ex
if MsgBox == 'yes':
root.destroy()
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:
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
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
Tutorials
Python Tutorials
R Tutorials
Julia Tutorials
MS Access Tutorials
Excel Tutorials
Privacy Policy
Terms of Service
https://datatofish.com/convert-text-file-to-csv-using-python-tool-included/ 7/7