convert a text data to csv python
<p>I have a long text file where i was able to extract these lines by grabing just the ones that contain the word "Average":</p>

<pre><code>Average time per layer:
Average Forward pass: 4013.65 ms.
Average Backward pass: 7425.13 ms.
Average Forward-Backward: 11480.2 ms.
</code></pre>

<p>Here is what I need in a csv file so I can easily make a graph:</p>

<pre><code>Average Forward pass  4013.65 
Average Backward pass 7425.13 
Average Forward-Backward 11480.2
</code></pre>

<p>Here is the output I'm getting:</p>

<pre><code>    : 7425.13 ms.
    : 11480.2 ms.
    : 
    : 4013.65 ms.
</code></pre>

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

<pre><code>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); 
</code></pre>

<p>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. </p>

<p>You can use a <a href="https://www.regex101.com/r/O4njRs/1" rel="nofollow">regex</a> on data like this:</p>

<pre><code>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
</code></pre>

python csv