Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a program in python that will process a text stream?

I am sorry if this is a repeat question. How do I write a python script to process data as a stream of line? I need to do this because the files that I am processing are huge, and I would rather not read the file into the memory.

I know that you can potentially read one line of the file at a time, but I want something that will process a text stream.

like image 812
Sam Avatar asked Nov 29 '22 18:11

Sam


1 Answers

You could just read the data from stdin, as described in this answer. This would look like that in code:

for line in sys.stdin:
    # do suff

If you want to process a file, then just call the script like this (on Unix platforms):

cat file.txt | python script.py

You can of course pipe the output of any other program in there too.

like image 197
Björn Pollex Avatar answered Dec 06 '22 18:12

Björn Pollex