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
  Is there anyway to make a .py file into a .exe file? Oshadha 2 119 Jan-13-2021, 04:58 AM
Last Post: Oshadha
  Sort data from JSON file Dummy_in_programming 2 100 Jan-04-2021, 06:17 PM
Last Post: deanhystad
  Python script to call oracle function and sftp result as pipe delimited text file tejkrishna 1 162 Dec-31-2020, 04:12 PM
Last Post: Larz60+
  Problems Sorting Data in an External File (.txt) Superlegend21 1 134 Dec-27-2020, 10:06 PM
Last Post: Superlegend21
  [split] How to convert the CSV text file into a txt file Pinto94 5 155 Dec-23-2020, 08:04 AM
Last Post: ndc85430
  How to fill parameter with data from multiple columns CSV file greenpine 1 101 Dec-21-2020, 06:50 PM
Last Post: Larz60+
  xml file creation from an XML file template and data from an excel file naji_python 1 119 Dec-21-2020, 03:24 PM
Last Post: Gribouillis
  How to use the count function from an Excel file using Python? jpy 2 165 Dec-21-2020, 12:30 AM
Last Post: jpy
  How to create new line '/n' at each delimiter in a string? MikeAW2010 3 218 Dec-15-2020, 05:21 PM
Last Post: snippsat
  how to automate sending bulk emails from an excel file or a calc-file apollo 0 162 Dec-10-2020, 08:43 PM
Last Post: apollo

Forum Jump:

User Panel Messages

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