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
273 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
• 2,980 points

edited Jun 26, 2019 by Kalgi 504 views
0 votes
0 answers
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
1,617 views
0 votes
1 answer

How to Convert usual text to executable machine code in python

what you are looking for is a ...READ MORE

answered Oct 1, 2018 in Python by Priyaj
• 57,530 points
780 views
+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 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 950 views
0 votes
0 answers
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,250 points
86 views