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
#2
This was fixed my issue

Tried using front slash / in string literal in save_path.

According to OP input, there are some blank rows, so took care of that with len(row)>0 condition.
Reply
#3
(Apr-21-2019, 12:36 PM)Chinmay Wrote: This was fixed my issue

Tried using front slash / in string literal in save_path.

According to OP input, there are some blank rows, so took care of that with len(row)>0 condition.

I think you can just rename text files in mac OS and turn txt files to csv. Or use Microsoft Excel to do that also.

But...

Where I work we would use this method:
import os
import pandas as pd

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

in_filename = os.path.join(save_path,'Input.txt')
out_filename = os.path.join(save_path,'Output.csv')

df = pd.read_csv(in_filename, sep=";")
df.to_csv(out_filename, index=False)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  file open "file not found error" shanoger 8 1,070 Dec-14-2023, 08:03 AM
Last Post: shanoger
  Need to replace a string with a file (HTML file) tester_V 1 752 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 915 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,086 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,107 Dec-15-2022, 04:32 PM
Last Post: Larz60+
Photo Making Zip file of a file and Directory Nasir 2 1,013 Oct-07-2022, 02:01 PM
Last Post: Nasir
  Need Help: Convert .pcl file to .pdf file ManuRaval 6 2,531 Sep-13-2022, 01:31 PM
Last Post: ManuRaval
  filter every 24 days file (considering file name) RolanRoll 5 2,203 Jun-23-2022, 11:41 AM
Last Post: RolanRoll
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,644 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,766 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