Reputation: 171
I have a file abcd.txt
containing:
"""
hello,123 [1231,12312]1231231
hello, world[3r45t,3242]6542
123 213 135
4234 gdfg gfd 32
sd23 234 sdf 23
hi, hello[234,23423]561
hello, hi[123,123]985
"""
I want to print the string which is after the second ',' character till the ']'. My output should be:
12312
3242
23423
123
I tried this:
def select(self):
file = open('gis.dat')
list1 = []
for line in file:
line = line.strip()
if re.search('[a-zA-Z]',line):
list1.append(line.partition(',')[-1].rpartition(']')[0])
return list1
Upvotes: 3
Views: 285
Reputation: 99001
You may use:
import re
for line in open("abcd.txt"):
match = re.findall(r".*?,.*?,(\d+)", line)
if match:
print match[0]
Output:
12312
3242
23423
123
Upvotes: 3