Skip to content
Advertisement

python filter lines matching keyworks

I have working python script producing following output.

JavaScript

Script output

JavaScript

Now i want want any line starting with stream in output so i am using following logic but its not working.

JavaScript

Advertisement

Answer

Your data is a massive string instead of a list of strings as you expected. Also it’s easier to use str.startswith instead of regex (less resource hungry). Simply change your code as follows:

JavaScript

Where n is the total number of lines you have in your data. If you only wanted to count the specific times you encountered "stream":

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement