2

I have a StringIO object the is filled correctly. I than have the following code:

val = log_fp.getvalue()
lines = val.split('\n') 
newval = ''
for line in lines:
    if (not line.startswith('[output]')):
        newval = line   
        print 'test1'+newval    
print 'test2' +newval

in the loop, I have the correct value for newval printed, but in the last print, I have an empty string. Any ideas what I am doing wrong? What I need is to extract one of the lines in the stringIO object that is marked [output], but newval seems to be empty in 'test2'.

4
  • Have you tried adding single quotes around newval? That way, you could see if python is printing a newline character. Commented Feb 3, 2011 at 15:58
  • @David, the split() shouldn't be including newline chars in the elements of lines Commented Feb 3, 2011 at 16:03
  • do you want to extract only one line marked '[output]' or all lines marked as such? Commented Feb 3, 2011 at 16:12
  • Can you give an example input and output for your program? Seems like you program shouldn't work, as it takes the lines that DON'T start with '[output]'. Commented Feb 3, 2011 at 17:01

3 Answers 3

4

Splitting on '\n' for a string such as 'foo\n' will produce ['foo', ''].

Sign up to request clarification or add additional context in comments.

3 Comments

sounds like it could be the problem, lines = val.splitlines() will fix it.
again, it could be the reason, but the output will have a line test1. wouldn't it?
The 'not' logic is problematic. It overwrites newval with anything that doesn't start with '[output]'. We also don't know if the lines have leading whitespace ' [output]'. Providing sample input/output always helps with this type of question. @Jochen's advice would alter the behaviour on the leading space too (for the better).
1

What I need is to extract one of the lines in the stringIO object that is marked [output],

Untested:

content = log_fp.getvalue().split()
output_lines = [x for x in content if x.startswith('[output'])]

Then get the first element of output_lines, if that is what you need.

Comments

0

Is log_fp a text file? If so, the last value in lines will be everything after the last newline character. Your file probably terminates in a newline, or a newline and some whitespace. For the former case, the last value of line will be an empty string. For the latter case, the last value of line will be the whitespace.

To avoid this, you could add a new clause to the if statement to check the trimmed string is not empty, eg.

val = log_fp.getvalue()
lines = val.split('\n') 
newval = ''
for line in lines:
    if ( len(line.strip()) > 0):
        if (not line.startswith('[output]')):
            newval = line   
            print 'test1'+newval    
print 'test2' +newval

(I haven't tried running this, but it should give you the idea)

2 Comments

why should it matter? OP should get 'test1' as the last of test1-outputs. that's all. It still is out there, no?
test1 output will give the last line which does not start with [output] - in his example, that could be an empty string for the case where the file ends in a newline.

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.