Python Forum
.Txt file to .CSV file conversion
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
.Txt file to .CSV file conversion
#1
Hi Team,
I am working on below python script which task is "It will take whatever data present in a .txt file and load it into in a .csv file as output.My code is working but the issue is everything is coming as rows in CSV output.

Below is my code:-
# - *- coding: utf- 8 - *-

import os.path
import csv

save_path = "C:\Users\desktop\Python-testing\"

completeName_in  = os.path.join(save_path,'Input_file'+'.txt')
completeName_out = os.path.join(save_path,'Output_file_csv'+'.csv')

 
file1=open(completeName_in)
In_text = csv.reader(file1,delimiter = ';')

file2 =open(completeName_out,'w')
out_csv = csv.writer(file2)

file3 = out_csv.writerows(In_text)

file1.close()
file2.close()
---------------------------------------------------------------------------
I have data in input .txt file:-

Output:
Line no: 1 Line: This is Line1 Line no: 2 Line: This is Line2 Line no: 3 Line: This is Line3
----And I am getting below as output in .csv format:-

Output:
Line no: 1 Line: This is Line1 Line no: 2 Line: This is Line2 Line no: 3 Line: This is Line3
-----------whereas I need help in getting data like this:-
Output:
Line no: Line: 1 This is Line1 2 This is Line1 3 This is Line1
P.S:- I am unable to attach either csv output file or any screenshot.Please bear with me.
need your valuable suggestions.

Thak you
Chinmay Nayak
Reply


Messages In This Thread
.Txt file to .CSV file conversion - by Chinmay - Jan-14-2018, 08:00 AM
RE: .Txt file to .CSV file conversion - by Chinmay - Apr-21-2019, 12:36 PM
RE: .Txt file to .CSV file conversion - by rxndy - Apr-21-2019, 06:45 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  file open "file not found error" shanoger 8 1,210 Dec-14-2023, 08:03 AM
Last Post: shanoger
  Need to replace a string with a file (HTML file) tester_V 1 796 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  How can I change the uuid name of a file to his original file? MaddoxMB 2 972 Jul-17-2023, 10:15 PM
Last Post: Pedroski55
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,143 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,157 Dec-15-2022, 04:32 PM
Last Post: Larz60+
Photo Making Zip file of a file and Directory Nasir 2 1,058 Oct-07-2022, 02:01 PM
Last Post: Nasir
  Need Help: Convert .pcl file to .pdf file ManuRaval 6 2,597 Sep-13-2022, 01:31 PM
Last Post: ManuRaval
  filter every 24 days file (considering file name) RolanRoll 5 2,305 Jun-23-2022, 11:41 AM
Last Post: RolanRoll
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,739 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  How to split file by same values from column from imported CSV file? Paqqno 5 2,857 Mar-24-2022, 05:25 PM
Last Post: Paqqno

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020