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:
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.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
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 ) |