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
  How to Randomly Print a Quote From a Text File When User Types a Command on Main Menu BillKochman 13 6,751 Dec-16-2025, 05:29 PM
Last Post: zodazy
Question [SOLVED] Linefeed when writing "f" strings to text file? Winfried 5 759 Nov-04-2025, 11:51 AM
Last Post: buran
  .xlsm file can't be opened after writing data to one worksheet mhyatt 4 734 Oct-11-2025, 11:25 PM
Last Post: Pedroski55
  How to space data on x axis m_kdch 2 2,792 Sep-11-2025, 05:35 AM
Last Post: Pedroski55
  how to write/overwrite data in a txt. file according to inp Quinn 2 1,543 Aug-12-2025, 04:20 PM
Last Post: Quinn
Question [SOLVED] Open file, and insert space in string? Winfried 7 2,467 May-28-2025, 07:56 AM
Last Post: Winfried
  How can I write formatted (i.e. bold, italic, change font size, etc.) text to a file? JohnJSal 13 35,426 May-20-2025, 12:26 PM
Last Post: hanmen9527
  How to remove unwanted images and tables from a Word file using Python? rownong 2 2,155 Feb-04-2025, 08:30 AM
Last Post: Pedroski55
  Best way to feed python script of a file absolut 6 2,459 Jan-11-2025, 07:03 AM
Last Post: Gribouillis
  Removal of watermark logo pdf file Python druva 0 1,532 Jan-01-2025, 11:55 AM
Last Post: druva

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.