0

I have a file with a large amount of random strings contained with in it. There are certain patterns that I wan't to remove, so I decided to use RegEX to check for them. So far this code, does exactly what I want it to:

#!/usr/bin/python

import csv
import re
import sys
import pdb


f=open('output.csv', 'w')

with open('retweet.csv', 'rb') as inputfile:
    read=csv.reader(inputfile, delimiter=',')
    for row in read:
        f.write(re.sub(r'@\s\w+', ' ', row[0]))
        f.write("\n")
f.close()

f=open('output2.csv', 'w')

with open('output.csv', 'rb') as inputfile2:
    read2=csv.reader(inputfile2, delimiter='\n')
    for row in read2:
        a= re.sub('[^a-zA-Z0-9]', ' ', row[0])
        b= str.split(a)
        c= "+".join(b)
        f.write("http://www.google.com/webhp#q="+c+"&btnI\n")
f.close()

The problem is, I would like to avoid having to open and close a file as this can get messy if I need to check for more patterns. How can I perform multiple re.sub() calls on the same file and write it out to a new file with all substitutions?

Thanks for any help!

1
  • csv.reader(..., delimiter='\n')? Why not just read the file line by line instead? for line in inputfile2: is enough.. Commented Oct 15, 2013 at 17:08

1 Answer 1

3

Apply all your substitutions in one go on the current line:

with open('retweet.csv', 'rb') as inputfile:
    read=csv.reader(inputfile, delimiter=',')
    for row in read:
        text = row[0]
        text = re.sub(r'@\s\w+', ' ', text)
        text = re.sub(another_expression, another_replacement, text)
        # etc.
        f.write(text + '\n')

Note that opening a file with csv.reader(..., delimiter='\n') sounds awfully much as if you are treating that file as a sequence of lines; you could just loop over the file:

with open('output.csv', 'rb') as inputfile2:
    for line in inputfile2:
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.