I apologize if this is a repeated question. How to write a python script to process data as a string stream? I need to do this because the files I process are huge, and I prefer not to read the file in memory.
I know that you can read one line of the file at a time, but I want something that will handle the text stream.
You can simply read the data from stdinas described in this. It will look like this:
stdin
for line in sys.stdin: # do suff
If you want to process the file, just call the script as follows (on Unix platforms):
cat file.txt | python script.py
You can, of course, also connect the output of any other program.
, . :
python script.py file1.txt file2.txt file3.txt file4.txt
script.py
import fileinput for line in fileinput.input(): # do stuff here
fileinput - , . Space_C0wb0y :
python script.py - < file.txt
cat file.txt | python script.py -
fileinput , Space_C0wb0y, , , .
f = open('somefile.txt') for line in f: process(line)
In fact, it fcan be anything that is iterable, for example, a list of lines or even sys.stdinif you want to read from standard input.
f
sys.stdin