1

I have a file, 'in.csv', which has many entries like the following:

[Part 1: A]
Was hab’ ich nur getan?
Hab mich selbst nie hinterfragt
Wer bin ich, wer bin ich, wer bin ich, was bin ich bloß?
Aaaah - wer bin ich, was bin ich bloß?
Ging es all die Zeit lang wirklich nur um mich?
Nur um mich, nur um mich, nur um mich?
Denk’ ich wirklich immer nur an mich?
Nur an mich, nur an mich, nur an mich?

[Part 2: T]
Ich bin so horrible, horrible, horrible
Fehler einzusehen ist mir nicht possible, possible, possible
Jaaah - doch ich denk’ jeden Tag an all den Shit, den ich getan hab’
Warum unterschätz’ ich meinen Impact auf die andern?
Homie, sag mir, warum ficke ich mein Karma
Und geb kein’ Fick auf alles, was die Stimme in mir labert?
Blockiert mich da mein Ego oder weiß ich es nicht besser?
Mann, wann verdammt fühl ich mich bereit etwas zu ändern?
Warum lass’ ich einfach nichts mehr an mich ran?
Warum lass’ ich selbst mich selbst nicht an mich ran?
,1

(...)

and I want to find and eliminate all occurrences of this pattern:

match = "[\(\[].*?[\)\]]"

I'm trying this:

with open('in.csv', 'rb') as in_file, open('out.csv', 'wb') as out_file:
     reader = csv.reader(in_file, delimiter='\t')
     for item in list(reader):
         re.sub(match, ' ', item, flags=re.MULTILINE)      
         out_file.write(item)

but it is not working. how do I go about finding and eliminating all []?

1 Answers1

1

The problem is you're doing nothing with the result, the expression is correct:

with open('in.csv', 'rb') as in_file, open('out.csv', 'wb') as out_file:
     reader = csv.reader(in_file, delimiter='\t')
     for item in list(reader):
         item = re.sub(match, ' ', item, flags=re.MULTILINE) 
         # ---^---     
         out_file.write(item)

The sub function does not change the original string but rather gives back a new copy, so you'll need to catch this. You can even ameliorate your expression by adding anchors:

^[\(\[].*?[\)\]]

It reduces the steps needed dramatically (10 times!), see a demo on regex101.com.

Jan
  • 42,290
  • 8
  • 54
  • 79