Python convert txt file to csv format with rows and columns

+1 vote

 How to convert txt file to csv format with rows and columns? The file is the following format:

account_number,name,item_code,category,quantity,unit price,net_price,date
296809,Carroll PLC,QN-82852,Belt,13,44.48,578.24,2014-09-27 07:13:03
098022,Heidenreich-Bosco,MJ-21460,Shoes,19,53.62,1018.78,2014-07-29 02:10:44
563905,"Kerluke, Reilly and Bechtelar",AS-93055,Shirt,12,24.16,289.92,2014-03-01 10:51:24
093356,Waters-Walker,AS-93055,Shirt,5,82.68,413.40,2013-11-17 20:41:11
659366,Waelchi-Fahey,AS-93055,Shirt,18,99.64,1793.52,2014-01-03 08:14:27
563905,"Kerluke, Reilly and Bechtelar",AS-93055,Shirt,17,52.82,897.94,2013-12-04 02:07:05
995267,Cole-Eichmann,GS-86623,Shoes,18,15.28,275.04,2014-04-09 16:15:03
524021,Hegmann and Sons,LL-46261,Shoes,7,78.78,551.46,2014-06-18 19:25:10
929400,"Senger, Upton and Breitenberg",LW-86841,Shoes,17,38.19,649.23,2014-02-10 05:55:56
563905,"Kerluke, Reilly and Bechtelar",KV-99194,Shirt,12,26.98,323.76,2014-05-20 00:21:28
995267,Cole-Eichmann,KV-99194,Shirt,19,60.22,1144.18,2014-03-10 06:23:31
524021,Hegmann and Sons,QN-82852,Belt,6,13.12,78.72,2013-11-03 18:38:16
758133,"Kihn, McClure and Denesik",LL-46261,Shoes,4,59.69,238.76,2014-01-11 21:48:28
555594,"Ernser, Cruickshank and Lind",FK-71853,Shirt,12,97.25,1167.00,2014-09-19 13:20:00
201259,Koelpin PLC,GS-86623,Shoes,9,81.44,732.96,2014-08-12 08:05:27
093356,Waters-Walker,LL-46261,Shoes,18,53.33,959.94,2014-07-15 23:21:11
563905,"Kerluke, Reilly and Bechtelar",KV-99194,Shirt,4,35.62,142.48,2014-10-05 23:38:16
201259,Koelpin PLC,KV-99194,Shirt,17,98.23,1669.91,2014-01-26 01:52:36
758133,"Kihn, McClure and Denesik",WJ-02096,Belt,15,69.52,1042.80,2013-11-13 21:38:46
296809,Carroll PLC,VG-32047,Shirt,12,80.12,961.44,2014-05-24 16:03:28
750461,"Volkman, Goyette and Lemke",WJ-02096,Belt,13,81.19,1055.47,2014-01-08 02:45:07
929400,"Senger, Upton and Breitenberg",LL-46261,Shoes,2,48.15,96.30,2014-04-28 07:01:04
Aug 1, 2019 in Python by Jisha
57,481 views

4 answers to this question.

0 votes

This code should work. You have to take the input from text file and separate the values based on commas (,). 

import pandas as pd

df = pd.read_csv("Demo.txt",delimiter=',')
df.to_csv('Demo1.csv')


Hope this helps!!

If you need to know more about Python, join Python online course today.

Thanks!

answered Aug 1, 2019 by Ashish
Hi@ANAND,

When you read your dataset using pandas dataframe, it will automatically convert your dataset in rows and columns. So if your dataset doesn't have (,) then don't use delimiter function.
0 votes
If there is lots of raw data text file then you can automate that by using below given program:

import pandas as pd

x = str(input("Enter the txt format filename :"))
y = str(input("Enter the csv format filename to save :"))

df = pd.read_csv("{}".format(x),delimiter=",")
df.to_csv("{}".format(y))
answered Sep 21, 2020 by Archies singh
how do you do it without implementing special modules like pandas
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 pandas' package. You may use the following command to install the pandas 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: Convert the text file to CSV using Python

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 Dec 14, 2020 by Rajiv
• 8,870 points
0 votes

Python will read data from a text file and will create a data frame with rows equal to the number of lines present in the text file and columns equal to the number of fields present in a single line. See the below example for a better understanding.

Note: The first column in the data frame is indexing which is by default when a text file is read.

Once the data frame is created, we will store this data frame into a CSV file format using Dataframe.to_csv() Method.

Python3

filter_none



brightness_4
# importing panda library

import pandas as pd

  

# readinag given csv file

# and creating dataframe

dataframe1 = pd.read_csv("Edureka.txt")

  

# storing this dataframe in a csv file

dataframe1.to_csv('Edureka.csv', 

                  index = None)

answered Dec 14, 2020 by Gitika
• 65,770 points

Related Questions In Python

+1 vote
2 answers

Python convert XLS and XLSX file to csv

XLSX tables are usually created in MS ...READ MORE

answered Aug 30, 2019 in Python by Mian Tanzeel
18,134 views
0 votes
1 answer

Python: Copy names from txt file to csv format

Hi@ANAND, It depends on your use case. In ...READ MORE

answered Jun 10, 2020 in Python by MD
• 95,460 points
1,456 views
0 votes
2 answers

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

Steps to Convert Text File to CSV ...READ MORE

answered Oct 15, 2020 in Python by Abdul Shekh
9,232 views
–1 vote
1 answer

Python convert excel file to csv

Here you go: import glob path_to_excel_files = glob.glob('path/to/excel/files/*.xlsx') for ...READ MORE

answered Feb 8, 2019 in Python by Omkar
• 69,220 points
1,271 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,434 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,067 views
0 votes
1 answer

Need a python script to login into a website but username and password are in a txt file (Selenium automation script)

I had a similar requirement. I had ...READ MORE

answered Jan 31, 2019 in Python by Omkar
• 69,220 points
8,313 views
+1 vote
2 answers

Python convert extracted excel file to csv

Some services require table data in CSV ...READ MORE

answered Aug 30, 2019 in Python by Mian Tanzeel
6,974 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP