How do I convert text file to CSV file with only plain python Meaning no panda or any other special module

0 votes
I'm trying to implement slice string into a text to csv converter and my document has rows and columns. I'm very new to coding and even newer to python so im unsure ow to go about this. Although i'm trying to use plain python and not use any odules or library functions like pandas and etc. I'd really appreciate some help! Please and Thank you!
Oct 15, 2020 in Python by anonymous
• 120 points

2 answers to this question.

0 votes

Hi, @There,

Regarding your query, I would suggest you go through this   It has multiple solutions given that you will need to solve your Query. 

I hope it helps!!!

answered Oct 15, 2020 by Gitika
• 65,910 points
Hi there! The answers only included using special modules (ie panda) I appreciate the help tho!
0 votes

Steps to Convert Text File to CSV using Python

Step 1: Install the panda's package

If you haven’t already done so, install the panda's package. You may use the following command to install the panda's 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:

Finally, you may use the template below in order to facilitate the conversion of your text file to CSV:

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:

answered Oct 15, 2020 by Abdul Shekh
Hi there im sorry i can't use pandas


If you are  trying to convert that text into a .csv (table) using a python script, then you can follow  this:

You need to split the line first.

import csv

with open('log.txt', 'r') as in_file:
    stripped = (line.strip() for line in in_file)
    lines = (line.split(",") for line in stripped if line)
    with open('log.csv', 'w') as out_file:
        writer = csv.writer(out_file)
        writer.writerow(('title', 'intro'))

Related Questions In Python

0 votes
0 answers
–1 vote
2 answers
0 votes
1 answer

How do I convert a Python program to a runnable .exe Windows program?

Understand that every 'freezing' application for Python ...READ MORE

answered Oct 13, 2018 in Python by SDeb
• 13,300 points
0 votes
1 answer

How do I use urllib to see if a website is 404 or 200 in Python?

For Python 3, try doing this: import urllib.request, ...READ MORE

answered Nov 29, 2018 in Python by Nymeria
• 3,560 points

edited Dec 11, 2018 by Nymeria 13,378 views
0 votes
1 answer

How do I read a file on only read only mode in python?

Hey @David! TRy something like this: campaign_data = ...READ MORE

answered Jun 20, 2019 in Python by Jason
+1 vote
4 answers

Python: convert txt file to csv format with rows and columns

Python will read data from a text ...READ MORE

answered Dec 14, 2020 in Python by Gitika
• 65,910 points
0 votes
0 answers

How to save the import csv file to mongodb using pyspark (or python)?

I have this code, and I want ...READ MORE

Oct 9, 2019 in Python by Ahmed
• 310 points
+1 vote
1 answer

Python: How to convert text to csv rows separated by a record separator?

Hi @Sumanth, try something like this: import csv with ...READ MORE

answered Dec 17, 2019 in Python by Pri
0 votes
1 answer

I want to download a file from the website by web scraping. Can anyone explain how to do this in jupyter lab (python) with an example?

Hey, Web scraping is a technique to automatically ...READ MORE

answered Apr 7, 2020 in Python by Gitika
• 65,910 points
0 votes
5 answers

How to read Pandas csv file with no header?

Use this logic, if header is present ...READ MORE

answered Mar 14, 2020 in Python by Shahabuddin
• 160 points
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP