
This tutorial will teach you how to convert a Text (.txt) file to CSV using the pandas library in Python. I’ll give you the complete source code of this application so you could easily test it out on your PC.
Let’s get started by installing the pandas library using the pip command.
pip install pandas
Now create a new Python file for this project and name it as code.py. After that, copy the below-given code-snippet and paste it in code.py.
# importing pandas library
import pandas as pd
# reading given csv from .txt file
# and creating dataframe
dataframe = pd.read_csv("file.txt", delimiter = ',')
# storing this dataframe in a csv file
dataframe.to_csv('new_file.csv', index = None)According to our code, we need to provide a file.txt that contains our data. By default, commas will be used as delimiters, but you can easily change them to whatever character you like.
So, let’s create a file.txt in the root folder of your project and add some comma-separated values. For example, have a look at this file.
ID, Name, Score 1, Furqan, 100 2, Ibrar, 95 3, Ammar, 87 4, Sheraz, 90 5, Babur, 65
python code.py
A file named new_file.csv will be generated automatically in your project’s root folder.
Here’s a screenshot of that file:
Google Chrome has dominated web browsing for over a decade with 71.77% global market share.…
Perplexity just made its AI-powered browser, Comet, completely free for everyone on October 2, 2025.…
You've probably heard about ChatGPT Atlas, OpenAI's new AI-powered browser that launched on October 21,…
Perplexity Comet became free for everyone on October 2, 2025, bringing research-focused AI browsing to…
ChatGPT Atlas launched on October 21, 2025, but it's only available on macOS. If you're…
Two AI browsers just entered the ring in October 2025, and they're both fighting for…