Python Forum
saving data from text file to CSV file in python having delimiter as space
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
saving data from text file to CSV file in python having delimiter as space
#1
I have a huge report file. I extracted the required data from it into a new file named "new.txt" I want to save this data in a csv file so that i get the columns and rows properly for each of the headers defined in the file. I am posting my code and the text file contents here...I am not able to get it in the proper form as we get it in excel. Kindly help..I am using python 2.7 and want to do this without using pandas package.

SIMPLE_FILE REPORT:
Output:
CALL Alias Severity File Line Wt Message +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ACT_99 ACT_99 Warning /application/XX/VV/2019_2_1/VV/2019.2/data/ip/xm/xm_123/ldm/xm_123.png 785 1000 message for this block ( syn_ff ) is ignored by syn ACT_99 ACT_99 Warning /application/XX/VV/2019_2_1/VV/2019.2/data/ip/xm/xm_123/ldm/xm_123.png 1111 1000 message for this block ( syn_ff ) is ignored by syn ACT_99 ACT_99 Warning /application/XX/VV/2019_2_1/VV/2019.2/data/ip/xm/xm_123/ldm/xm_123.png 1226 1000 message for this block ( syn_ff ) is ignored by syn ACT_99 ACT_99 Warning /application/XX/VV/2019_2_1/VV/2019.2/data/ip/xm/xm_123/ldm/xm_123.png 1354 1000 message for this block ( syn_ff ) is ignored by syn ACT_99 ACT_99 Warning /application/XX/VV/2019_2_1/VV/2019.2/data/ip/xm/xm_123/ldm/xm_123.png 1363 1000 message for this block ( syn_ff ) is ignored by syn
Here is my code.

import csv

outFile = "new.txt"
new_file = open(outFile, "a+")
new_file.truncate(0)
csv_file = "report.csv"
    
def open_file(filename):
    try:
        contents = [] 
        with open(filename, 'r') as f1:
            contents=[line.strip() for line in f1]
        counter = contents.index("Final report:")
        for item in contents[counter:]:
            new_file.write(item+"\n")

        in_txt = csv.reader(new_file, delimiter = '\t')
        out_csv = csv.writer(open(csv_file, 'w'))

        out_csv.writerows(in_txt)
    except Exception,e:
        print str(e)
    exit(1)
Reply
#2
Please use the python tags for your code. Python2 is very old and no longer maintained. You should be using python3 for any new projects.

What part isn't the proper form? Can you give an example of the input?

I see a definition for open_file(), but I don't see anywhere that it's called.

Why are you opening new_file in append mode and then immediately truncating it? Why not just open it in write mode?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  very newbie problem on text file zapad 2 162 Apr-12-2024, 06:50 PM
Last Post: zapad
  How to Randomly Print a Quote From a Text File When User Types a Command on Main Menu BillKochman 12 571 Apr-12-2024, 11:51 AM
Last Post: deanhystad
  encrypt data in json file help jacksfrustration 1 180 Mar-28-2024, 05:16 PM
Last Post: deanhystad
  Python openyxl not updating Excel file MrBean12 1 306 Mar-03-2024, 12:16 AM
Last Post: MrBean12
  Python logging RotatingFileHandler writes to random file after the first log rotation rawatg 0 374 Feb-15-2024, 11:15 AM
Last Post: rawatg
  connect sql by python using txt. file dawid294 2 422 Jan-12-2024, 08:54 PM
Last Post: deanhystad
  file open "file not found error" shanoger 8 1,065 Dec-14-2023, 08:03 AM
Last Post: shanoger
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,407 Nov-09-2023, 10:56 AM
Last Post: mg24
  Replace a text/word in docx file using Python Devan 4 3,212 Oct-17-2023, 06:03 PM
Last Post: Devan
  Input network device connection info from data file edroche3rd 6 988 Oct-12-2023, 02:18 AM
Last Post: edroche3rd

Forum Jump:

User Panel Messages

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