how to feed string to pyparsing line by line?
pyparsing, python
Solution
You could do :
with open(filename) as f:
for line in f:
PyparsingGrammar.parseString(line)
using the keyword `with` automatically closes the file once you are done, and gives you a handle to work with.
for x in something:
do_something
is a standard way of going over iterables (stuff which can be iterated, e.g: `list, tuple, dictionary` in Python.
I forgot to mention, but I guess you figured it: when you open a file in Python with `with open(filename) as f` you are getting a `list` where each line in the list is an item. That is why you are able to treat `f` as an iterator.
Problem
I encounter this question when I want to parse a file with large size using pyparsing. I have already created the pyparsing grammar for the whole file. But I am not sure how to feed the string to the parser line by line by reading this big file. Currently I am using the the method like: ``` pyparsingGrammer = some pyparsing grammar I created PyparsingGrammar.parseString(open(filename).read()) ``` Except the memory usage for the big `read()`, another motivation for me to go for line feeding is to extend my parser to a realtime case where info is feeded to the parser one line followed by another.