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:
[...]What's a neat way to do something similar with a text file in python?
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', '']
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With