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 in Python by anonymous
• 120 points
153 views

2 answers to this question.

0 votes

Hi, @There,

Regarding your query, I would suggest you go through this https://www.edureka.co/community/53835/python-convert-txt-file-to-csv-format-with-rows-and-columns   It has multiple solutions given that you will need to solve your Query. 

I hope it helps!!!

answered Oct 15 by Gitika
• 49,540 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 by Abdul Shekh
Hi there im sorry i can't use pandas

Hey,

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'))
        writer.writerows(lines)

Related Questions In Python

0 votes
0 answers
0 votes
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 12, 2018 in Python by SDeb
• 13,250 points
285 views
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,520 points

edited Dec 11, 2018 by Nymeria 6,540 views
0 votes
2 answers

How to read Pandas csv file with no header?

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

answered Mar 14 in Python by Shahabuddin
• 160 points
34,676 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
120 views
+1 vote
2 answers

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

If there is lots of raw data ...READ MORE

answered Sep 21 in Python by Archies singh
29,079 views
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
839 views
0 votes
1 answer
0 votes
1 answer

How do I install a Python package with a .whl file?

I just used the following which was ...READ MORE

answered 5 days ago in Python by Gitika
• 49,540 points
27 views