A module that makes it easier to write Perl-like filter programs in Python fileinput

Personally, I think the thing that attracts me to Perl is that you can easily write a filter program using while (<>).

#!/usr/bin/perl

while (<>) {
  y/a-z/A-Z/;
  print;
}

This completes the filter program for uppercase lowercase letters. You can specify as many files as you like with arguments, and if there are no arguments, read from standard input.

$ echo hoge | ./toupper.pl
HOGE
$ ./toupper.pl hoge.txt hoge2.txt
HOGE
HOGE2
$

You can do the same in Python with the fileinput module.

#!/usr/bin/python
import fileinput

for line in fileinput.input():
    print line.upper(),

If you specify "-" as the file name, it will be the same as the standard input.

See also: https://docs.python.org/2/library/fileinput.html

Recommended Posts

A module that makes it easier to write Perl-like filter programs in Python fileinput
A story that makes it easier to see Model debugging in the Django + SQLAlchemy environment
I wrote a class that makes it easier to divide by specifying part of speech when using Mecab in python
To add a module to python put in Julialang
I made a module in C language to filter images loaded by Python
Try to make a Python module in C language
I want to write in Python! (2) Let's write a test
[Python] How to write a docstring that conforms to PEP8
Deep nesting in Python makes it hard to read
A Python script that crawls RSS in Azure Status and posts it to Hipchat
How to write a metaclass that supports both python2 and python3
I made a tool that makes it a little easier to create and install a public key.
A story that makes it easy to estimate the living area using Elasticsearch and Python
Recursively get the Excel list in a specific folder with python and write it to Excel.
[Python] I wrote a test of "Streamlit" that makes it easy to create visualization applications.
How to write a Python class
Write A * (A-star) algorithm in Python
Write a pie chart in Python
Write a vim plugin in Python
Write a depth-first search in Python
A story that didn't work when I tried to log in with the Python requests module
・ <Slack> Write a function to notify Slack so that it can be quoted at any time (Python)
Make it easier to test programs that work with APIs with vcrpy
I made a web application in Python that converts Markdown to HTML
I made a Discord bot in Python that translates when it reacts
I tried to develop a Formatter that outputs Python logs in JSON
How to write a string when there are multiple lines in python
I made a tool that makes decompression a little easier with CLI (Python3)
Write the test in a python docstring
Write a short property definition in Python
How to get a stacktrace in python
Write a Caesar cipher program in Python
Module to generate word N-gram in Python
Write a simple greedy algorithm in Python
Write a simple Vim Plugin in Python 3
How to write Ruby to_s in Python
Note that cibuildwheel builds python bwheel (including C ++ module) in bulk with CI and uploads it to PyPI
I tried to create a class that can easily serialize Json in Python
Create a plugin that allows you to search Sublime Text 3 tabs in Python
I want to create a priority queue that can be updated in Python (2.7)
[Beginner] What happens if I write a program that runs in php in Python?
[Python] Concatenate a List containing numbers and write it to an output file.
Searching for an efficient way to write a Dockerfile in Python with poetry
Try to calculate a statistical problem in Python
How to clear tuples in a list (Python)
A memo that I wrote a quicksort in Python
To execute a Python enumerate function in JavaScript
How to embed a variable in a python string
python> How to write multiple lines> How to write in mnist_with_summaries.py> It seems that there is no need for line concatenation symbols
I want to create a window in Python
How to create a JSON file in Python
A program that removes duplicate statements in Python
Publish a Python module that calculates meteorological factors
A clever way to time processing in Python
How to add a Python module search path
Steps to develop a web application in Python
Use cryptography module to handle OpenSSL in Python
The trick to write flatten concisely in python
Why does Python have to write a colon?
How to notify a Discord channel in Python
Let's write a Python program and run it