简体   繁体   中英

How to convert multiple lines of output to a single multi-line output

This seems to be a really easy one, but I was not able to find the answer myself. I have a function returns many lines of output:

... if lines[i].strip().find('OPEN_MODE') == 0:
            res = lines[i:i + 3]
            for i in res:
                yield i.strip()
            print('')
for inp in db_status(remote_conn):
    print(int)
> DATABASE_STATUS
> -----------------
> ACTIVE ...

The next step is to pass the whole output to another function as a multiline string, eg:

'''DATABASE_STATUS
-----------------
ACTIVE...'''

I have tried to use splitlines() on int, or join the strings with newlines '\\n'.join() but it did not work out.

If you're just asking "how do I get all the yielded values from my function into a single string?", then try using join :

result = "\n".join(db_status(remote_conn))

I know you said that you tried "\\n".join already and it didn't work, but I'm guessing you passed in the wrong argument, for example "\\n".join(inp) within the for loop. With this, you don't actually need a for loop outside of the function at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM