Python How to convert text to csv rows separated by a record separator

+1 vote

We have data as below in a log file(jobs_log.txt)

Job : Job000001
Execution Date: 2010/01/01 00:05:00.00 PST Sun
App Name: App0001
Script : /oracle/dwh/scripts/test1.sh
***************************************************************
Job : Job000002
Execution Date: 2010/01/01 00:05:00.00 PST Sun
App Name: App0001
Script : /oracle/dwh/scripts/test2.sh
***************************************************************
....

we need to convert this data and write to a excel/csv file as shown in the below screenshot, if you observe row is separated with ***************************************************************

Dec 13, 2019 in Python by Sumanth
• 130 points
1,931 views

1 answer to this question.

+1 vote

Hi @Sumanth, try something like this:

import csv

with open('log.txt', 'r') as in_file:
    stripped = (line.strip() for line in in_file)
    lines = (line.split("***************************************************************") for line in stripped if line)
    with open('log.csv', 'w') as out_file:
        writer = csv.writer(out_file)
        writer.writerow(('Job Details'))
        writer.writerows(lines)
answered Dec 17, 2019 by Pri

Related Questions In Python

+1 vote
1 answer

How to convert records from csv to a list in Python?

If you are using Python 3.x then ...READ MORE

answered Jun 25, 2019 in Python by Arvind
• 3,050 points

edited Jun 26, 2019 by Kalgi 9,851 views
0 votes
0 answers
0 votes
2 answers

How do I convert text file to CSV file with only plain python. Meaning no panda or any other special module?

Steps to Convert Text File to CSV ...READ MORE

answered Oct 15, 2020 in Python by Abdul Shekh
9,235 views
0 votes
1 answer

How can I convert a list of dictionaries from a CSV into a JSON object in Python?

You could try using the AST module. ...READ MORE

answered Apr 17, 2018 in Python by anonymous
3,459 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,434 views
0 votes
1 answer
0 votes
1 answer

How to read a large file, line by line, in Python?

The correct, fully Pythonic way to read ...READ MORE

answered Jul 1, 2019 in Python by SDeb
• 13,300 points
868 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP