- Published on
Using the Python sys library to read data from stdin
- Authors
- Name
- Ruan Bekker
- @ruanbekker
Using Python's sys
library to read data from stdin
.
In this basic example we will strip our input, delimited by the comma character, add it to a list, and print it out
Python: Read Data from Standard Input
import sys
import json
mylist = []
data_input = sys.stdin.read()
destroy_newline = data_input.replace('\n', '')
mylist = destroy_newline.split(', ')
print("Stripping each word and adding it to 'mylist'")
print("Found: {} words in 'mylist'".format(len(mylist)))
for x in mylist:
print("Word: {}".format(x))
We will echo three words and pipe it into our python script:
$ echo "one, two, three" | python basic-stdin.py
Stripping each word and adding it to 'mylist'
Found: 3 words in 'mylist'
Word: one
Word: two
Word: three
Thank You
Thanks for reading, feel free to check out my website, feel free to subscribe to my newsletter or follow me at @ruanbekker on Twitter.
- Linktree: https://go.ruan.dev/links
- Patreon: https://go.ruan.dev/patreon