0

I have a long text file where i was able to extract these lines by grabing just the ones that contain the word "Average":

Average time per layer:
Average Forward pass: 4013.65 ms.
Average Backward pass: 7425.13 ms.
Average Forward-Backward: 11480.2 ms.

Here is what I need in a csv file so I can easily make a graph:

Average Forward pass  4013.65 
Average Backward pass 7425.13 
Average Forward-Backward 11480.2

Here is the output I'm getting:

    : 7425.13 ms.
    : 11480.2 ms.
    : 
    : 4013.65 ms.

Here is what i have but it doesn't give me the right results:

def parse_output(outputName):
    "This reads the parsed file and formated it to a map"
    with open(outputName,'r') as parsedFile:
        entry = {}
        for line in parsedFile:
            key, value = map(line.strip, line.split(':',1))
            entry[key] = value
        yield entry

def print_csv(outputName, csvFile):
    "This reads the map and print it to csv"
    remove_file_exist(csvFile)
    for foo in parse_output(outputName):
        with open(csvFile, 'a') as csvFile:
            for entry in foo:
                csvFile.write(str(entry))
                print(entry)
            print(foo); 

I've tried to convert the original text to json, but didn't get it to work. any input will be appreciate it. I'm very very new to python. This is my first script in this language.

2
  • do you have the modules imported json for json csv for csv Commented Oct 4, 2016 at 17:45
  • So what went wrong? Are there mutliple items in this file? Are they consistent (that is, are they always in the same order?). Post enough of the data that we can get a few records to work with. Commented Oct 4, 2016 at 17:53

2 Answers 2

1

It would help to show us what output you are getting versus what you expect. However it looks to me like you are writing str(entry) to a file, where entry is a dict, and expecting it to render as a csv line. I suspect it will look a little more similar to one JSON object per line.

Whenever you want to write or read csv's in Python, the csv module is usually a good first stop. You might do better with something like this:

import csv

def parse_output(outputname):
    with open(outputname, 'r') as output:
        for row in output:
            yield [field.strip() for field in row.split(':', 1)]

def print_csv(outputname, csvname):
    with open(csvname, 'wb') as csvfile:
        csvfile = csv.writer(csvfile)
        for parsedline in parse_output(outputname):
            csvfile.write(parsedline)
Sign up to request clarification or add additional context in comments.

Comments

0

You can use a regex on data like this:

txt='''\
Average time per layer:
Average Forward pass: 4013.65 ms.
Average Backward pass: 7425.13 ms.
Average Forward-Backward: 11480.2 ms.'''

import re

for k,v in re.findall(r'^([^:]+):\s*(\d+\.\d+)', txt, re.M):
    print k, v

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.