0

I would like to save my results in my csv. But somehow it remains empty. But if I write the same output not in the csv and only print I get my results As I would like it to have.

import re
import glob
import os
## is the second script for matchParser.py, match the results for Duration in ms digit
lines = [line.strip() for line in open('output.csv')]
for result in lines:

    match = re.search(r'(!?\s.\d[Request completed in\s])', result)
    if match: print match.group(0)

but this version with the output_csv is not working, i dont know why..

import re
import glob
import os

lines = [line.strip() for line in open('output.csv')]
for result in lines:

    match = re.search(r'(!?\s.\d[Request completed in\s])', result)
    with open('outputREGEX5.csv', "w") as output_csv:
        if match: output_csv.write(match.group(0))

I also get the following error:

output.write(match.group(0))
AttributeError: 'NoneType' object has no attribute 'group'

Am grateful for any help.

-- EDIT My print Output:

 44 
 37 
 53 
 35 
 17 
 35 
 25 
 27 
 50 
 31 
 27 
 36 
 17 
 66 
 46 
 41 
 38 
 23 
 33 

1 Answer 1

4
open('outputREGEX5.csv', "w")

empties the file. Only open the file once outside of the loop, instead of opening it every iteration.

with open('outputREGEX5.csv', "w") as output_csv:
    for result in lines:
        match = re.search(r'(!?\s.\d[Request completed in\s])', result)
        if match: output_csv.write(match.group(0) + '\n')
Sign up to request clarification or add additional context in comments.

6 Comments

Can you please explain it a bit more precisely and say exactly where I should place it? I am a newbie in python
Almost... the match = ... line should be before the if match:, and at the same indent level.
its working! but the entrys are not in newline as in the print version. how can I add a \n So that every iteration is written into a new line?
@harun literally just by adding a \n to the thing that you're writing. Come on, give it a try.
I tried. Unfortunately without success. Always get this error: NameError: name 'match' is not defined :/
|

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.