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

2 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=',')
answered Aug 1, 2019 by Ashish

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=",")
answered Sep 21 by Archies singh

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
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 in Python by MD
• 57,060 points
–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,030 points
0 votes
0 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 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,363 views
0 votes
0 answers
+4 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,800 points
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,030 points
+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