Welcome folks today in this blog post we will be converting the text file to csv
and saving it inside the local file system using the csv module
in python. All the full source code of the application is shown below.
Get Started
In order to get started you need to install the csv
module using the pip
command as shown below
pip install csv
After that you need to create the app.py
file and copy paste the following code
app.py
1 2 3 4 5 6 7 8 |
import csv with open('file.txt') as report: reader = csv.reader(report, delimiter="\\") with open('output.csv', 'w', newline='') as csvfile: writer = csv.writer(csvfile) for line in reader: writer.writerow(line) |
As you can see we are importing the csv
module at the very top of the file and then we are opening the input
text file called file.txt
and then we are reading it using the open()
method in python. And after that we are using the reader()
method of the csv module to convert the text file to csv using the delimiter. And then we are saving the output
csv file using the open()
method. And then we are writing the content of the text file to the csv output file using the writer()
and writerrow()
method