There is a code that works only with the data in the file.
import re f = open('12345') lines = f.readlines() f.close() f = open('12345', 'w') for line in lines: r = re.search(r'^.*("deactivated":"banned"|"deactivated":"deleted").*$', line).group(0) if line != r: f.write(line) print(r) f.close()
The problem is as follows. It is necessary to give the exact string in "r", I do this with the help of ".group (0)", but as soon as an exception is found, "r" becomes an object "None", everything crashes. I tried:
try: if line != r: f.write(line) print(r) except AttributeError: continue
No results. Doesn't want to catch the error in any way:
AttributeError: 'NoneType' object has no attribute 'group'
r'"deactivated":"(banned|deleted)"'
. - Alexey Ukolov.group(0)
completely useless - it’s enough to compare r with None, you are interested in the fact that there is no match, None means exactly this:if re.search(r'"deactivated":"(banned|deleted)"') == None
- Alexey Ukolovtry
line should be moved one line higher:try: r=re.searc(...).group(0)...
- andy.37