- Link to the python-file | - Link to the csv testdata file
import csv
import nltk
import re
from array import *
#Expressions
rgx_list = ['.', ',', ';', '\(', '\)', ':', '\.\.\.', '!']
#New empty array
ntitle = []
#Open a csv
with open('tripadvisor_dieburg.csv') as file:
reader = csv.DictReader(file)
#Get the title and replace the expressions
for row in reader:
for r in rgx_list:
new_title = row['title']
rgx = re.compile(r)
new_title = re.sub(rgx, '', new_title)
#Append to the array
ntitle.append(new_title)
#Print the new title
for n in ntitle:
print n
I created an array named rgx_list
for regular expressions and i opened a csv file with content. Then i tried to replace regular expressions in the titles row['title']
with a whitespace.
After that, i want to copy the new title into a new array named "ntitle".
Only '!' will be replaced in the string, but i want that all regular expressions will be replaced.
rgx_list = ['.', ',', ';', '\(', '\)', ':', '\.\.\.', '!']
Now, what i'm doing wrong?