Welcome folks today in this blog post we will be converting text file to csv
file inside python using the csv module. All the full source code of the application is given below.
Get Started
In order to get started we need to install the following library with the help of pip
command
pip install csv
After installing the library inside python project make an app.py
file and copy paste the following code
app.py
1 2 3 4 5 6 7 |
import csv with open('file.txt', 'r') as f: content = f.readlines() with open('test.csv', 'w+', newline = '') as csvFile: csvWriter = csv.writer(csvFile, delimiter = ' ') for elem in content: csvWriter.writerow([elem.strip()]) |
Now when you execute this python script by running the below command
python app.py
You will see the text file which is shown below will be converted to csv