1
abc=123
dabc=123
  abc=456
  dabc=789
    aabd=123

From the above file I need to find lines beginning with abc= (whitespaces doesn't matter)

in ruby I would put this in an array and do

matches = input.grep(/^\s*abc=.*/).map(&:strip)

I'm a totally noob in Python, even said I'm a fresh Python developer is too much.

Maybe there is a better "Python way" of doing this without even grepping ?

The Python version I have available on the platform where I need to solve the problem is 2.6

There is no way of use Ruby at that time

astropanic
  • 10,470
  • 19
  • 69
  • 131

2 Answers2

5
with open("myfile.txt") as myfile:
    matches = [line.rstrip() for line in myfile if line.lstrip().startswith("abc=")]
kindall
  • 168,929
  • 32
  • 262
  • 294
1

In Python you would typically use a list comprehension whose if clause does what you'd accomplish with Ruby's grep:

import sys, re
matches = [line.strip() for line in sys.stdin
           if re.match(r'^\s*abc=.*', line)]
user4815162342
  • 124,516
  • 15
  • 228
  • 298