Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert sed with multiple expressions to python

I have the following sed script:

cat foo.txt | sed -e "s/.*\[\([^]]*\)\].*/\1/g" -e "s/ //g" -e "s/'//g"

Which can be translated into three expressions:

  1. Captures all text between [...]
  2. Removes white-spaces
  3. Removes all single quotes

What's a neat way to do something similar with a text file in python?

like image 492
ajmartin Avatar asked Nov 18 '25 00:11

ajmartin


2 Answers

You could do it all with regular expressions (re.sub()) but this does it mostly with plain Python, just using regular expressions for the initial capture.

import re

s = "some string ['foo'] [b a r] [baz] [] extra stuff"

pat0 = re.compile(r'\[([^]]*)\]')

lst0 = pat0.findall(s)

lst1 = [s.replace(' ', '') for s in lst0]
lst2 = [s.replace("'", '') for s in lst1]

print(lst2) # prints: ['foo', 'bar', 'baz', '']
like image 171
steveha Avatar answered Nov 19 '25 12:11

steveha


import re

with open('foo.txt', 'r') as f:
    read_data = f.readlines()
    out_data = []
    for line in read_data:
        out_line = re.sub(r".*\[([^]]*)\].*", r"\1", line)
        out_line = re.sub(r" ", r"", out_line)
        out_line = re.sub(r"'", r"", out_line)
        out_data.append(out_line)
    # do whatever you want with out_data here
like image 23
mVChr Avatar answered Nov 19 '25 14:11

mVChr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!