Python convert txt file to csv format with rows and columns

527    Asked by bhagwatidubey in Python , Asked on Jul 12, 2021

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

Answered by Dhananjay Singh

This code should work for txt to csv python. You have to take the input from the 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')

Your Answer

Interviews

Parent Categories