Down below is Python word count script that I found online, I am having difficulty understanding how you would run this though. Would I need to create a file and open it in Python before running this program for it to work?
#!/usr/bin/env python
import sys
if __name__ == '__main__':
data = sys.stdin.read()
chars = len(data)
words = len(data.split())
lines = len(data.split('\n'))
print ("{0} {1} {2}".format(lines, words, chars))
Thanks for any help!
sys.stdin.read()
reads data from the console. Just run the python program and type whatever you want. When you're done press Ctrl + D
.
The sys.stdin.read()
line tells me that it is expecting to receive the input from the standard input so you can use it something like:
type somefile.txt | python wordcount.py
or run python wordcount.py
and type into the console ending with ctrl-d
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.