1

I am writing a python script which contains a list containing python print statements as a string. In my function, I am using a for loop to run exec function to run those statements.

Here is my function:

g_list = ["print('Wow!')\n", "print('Great!')\n", "print('Epic!')\n"]

def run_statements():
    for item in g_list:
        exec(item)

When I run run_statements() function, I get the following output:

Wow! 
Great! 
Epic!

Basically, I want to save the output as a string so that later, I can save it to my database.

Does anyone have any idea how can I do it?

EDIT: At the following question: python: get the print output in an exec statement He is trying to get output, My question is different in a way that I am trying to get output as a string

6
  • The point of print is to send text somewhere, not to save it in memory. If you want to save that as a string, you should simply do so, and then print it when appropriate. Also, it's likely that exec is the wrong approach for whatever goal you're trying to achieve. Commented Sep 13, 2016 at 1:18
  • Can you give an exact sample output of what you're expecting the final result to be? You said a string. So you want: "Wow!\nGreat!\nEpic\n"`? Commented Sep 13, 2016 at 1:18
  • @idjaw yes! thats exactly what I want Commented Sep 13, 2016 at 1:19
  • @idjaw it gives syntax error that way Commented Sep 13, 2016 at 1:23
  • @Elisha512 Look at the duplicate that was posted. Commented Sep 13, 2016 at 1:25

2 Answers 2

3

If you really need a print statement in the list of strings (as opposed to a print-like function with a different name, as suggested in another answer), you can reassign the name print to your own function, after carefully, carefully, carefully saving the old print function so you can carefully, carefully, carefully restore the name print to its proper definition. Like this:

>>> g_list = ["print('Wow!')\n", "print('Great!')\n", "print('Epic!')\n"]
>>> old_print = print
>>> def print(s): # redefining the built-in print function! terrible idea
...   global catstr
...   catstr += s
...
>>> catstr = ""
>>> for s in g_list: exec(s)
...
>>> catstr
'Wow!Great!Epic!'
>>> print = old_print # Don't forget this step!

This is completely immoral, and I did not advise you to do it. I only said you can do it.

To stress the inadvisability of this plan: exec should be used rarely; it is dangerous; reassigning the names of built-in functions to different functions should be done very rarely; it is dangerous. Doing both in the same code could really ruin your day, especially after a maintenance programmer edits your code "just a little," without realizing the potential impact.

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

12 Comments

thankyou @D-Von I understand the dangers of it but it might be something exactly what I am looking for. I will test it and if works alright will mark it as correct
this statement gives out error: old_print = print that invalid syntax
Are you on Python 3.x? That's where I ran the code. It won't work on Python 2.x, where print is a built-in command, not a function.
do you have any solution in mind for python 2. I am using python 2.7.11
it works perfectly correct in python 3. I have checked but it doesn't work when I include this in a function. I have added it as update to this answer. Can you please check?
|
1

If you want to save what you print, you could create a custom command, and save the output to a variable. For example

output_bin = ""

def printsave(string):
    print string
    if len(string) > 1:
        if string[-2] == "\n":
            output_bin = output_bin + string
        else:
            output_bin = output_bin + string + "\n"
    else:
        output_bin = output_bin + string + "\n

Then, whenever you call printsave() it is saved to the string output_bin. And if you want it to be saved to an array...

output_bin = []

def printsave(string):
    print string
    if len(string) > 1:
        if string[-2] == "\n":
            output_bin.append(string)
        else:
            output_bin.append(string + "\n")
    else:
        output_bin.append(string + "\n)

4 Comments

That's not valid Python syntax. This is not correct.
@idjaw Sorry, I didn't test it at all. :/
@idjaw What did I do wrong? If you would be as king as to tell me.
You can't use hyphens in Python. I strongly suggest testing the code to make sure it is a working solution as to not mislead the OP down a path of code that might not be working.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.