A Python script to execute Python code like "perl -ne"
I wrote a small utility script that can be used to run a small snippet of Python code like "perl -ne". I find it very useful for my needs. Hope it helps you too. Any suggestions welcome.
You can find the script as a gist here.
You can find the script as a gist here.
Comments
python
import sys
# Define the Python code to be executed on each line
python_code = '''
# Your Python code here
# Example: To print each line, you can use the following:
print(line, end='')
'''
# Function to execute Python code for each line of input
def execute_python_code():
for line in sys.stdin:
# Execute the Python code for each line
exec(python_code, {'line': line})
if __name__ == "__main__":
execute_python_code()