1

I am trying to replace multiple strings in a csv file. There is no specific column these strings may be in.

I used the example in this thread Python replace multiple strings, the wordpress entry. I thought I had it with the CSV module but I guess that does not have a replace function. I tried the below, but it only does the replace from the first dictionary. It has to do the entries in the first dictionary first, the order of the second dictionary doesn't matter as much.

def replace_all(text, dic):
    for i, j in dic.iteritems():
        text = text.replace(i, j)
    return text

f_dic = {'-':' '}
s_dic = {'  ':' ','   ':' ','SASD D':'SASD-D','DC W':'DC-W',r'PROD\s\d':r'PROD\d'}

with open('file1.csv','r') as f:
    text=f.read()
    with open('file2.csv','w') as w:
        text=replace_all(text,f_dic)
        text=replace_all(text,s_dic)
        w.write(text)

Can someone help me with this? or even better a csv module version of this?

Thanks, B0T

EDIT

This is my final code after the answer. It worked.

import re

def replace_all(text, dic):
    for i, j in dic.iteritems():
        text = text.replace(i, j)
    return text

f_dic = {'-':' '}
s_dic = {'  ':' ','   ':' ','SASD D':'SASD-D','DC W':'DC-W'}
t_dic = {'  ':' '}

with open('file1.csv','r') as f:
    text=f.read()
    text=replace_all(text,f_dic)
    text=replace_all(text,s_dic)
    text=replace_all(text,t_dic)
    text=re.sub(r'PROD\s(?=[1-9])',r'PROD',text)

with open('file2.csv','w') as w:
    w.write(text)
Community
  • 1
  • 1
CircuitB0T
  • 465
  • 2
  • 6
  • 19

1 Answers1

1

Hmm... this worked fine for me.

I'm running python 2.7.3

try a minimal test case starting with file1.csv containing "123abc":

def replace_all(text, dic):
    for i, j in dic.iteritems():
        text = text.replace(i, j)
    return text

f_dic = {'a':'d'}
s_dic = {'1':'x'}

with open('file1.csv','r') as f:
    text=f.read()
    with open('file2.csv','w') as w:
        text=replace_all(text,f_dic)
        text=replace_all(text,s_dic)
        print text
        w.write(text)

After running, file2.csv contained x23dbc

Perhaps my test case was too simple. How about you give us what you had in file1.csv, what you expected to see in file2.csv and what you actually saw in file2.csv.

MadDogMcNamara
  • 312
  • 1
  • 9
  • Actually, it looks like everything worked except for the ' ':' ' and the regex replace r'PROD\s\d':r'PROD\d'. Weird. I am using 2.7.5 – CircuitB0T Jul 08 '13 at 21:45
  • Not sure what you mean, are you saying that your original case worked, except the space replacement seems off? Can you give your input, expected output and actual output? – MadDogMcNamara Jul 08 '13 at 21:48
  • Well, it's a large file. An example of a string I want to change and is not changing is: "DRE United States PROD 2". I want to get rid of the extra spaces after the first word, and make "PROD 2" "PROD2". – CircuitB0T Jul 08 '13 at 21:53
  • Note: In the above there is supposed to be two spaces after "DRE". – CircuitB0T Jul 08 '13 at 21:54
  • Well according to your original rules, `"DRE "` should become `"DRE "`, and `"PROD 2"` would not change – MadDogMcNamara Jul 08 '13 at 21:59
  • Ohh, I thought I tested that regex, I must be tireder than I thought. I'm going to try this r'PROD\s(?=[1-9])':r'PROD'. You said the double space should be replaced by the single space in the original code? Thanks, BTW. – CircuitB0T Jul 08 '13 at 22:20
  • 1
    the replace method on a string will NOT do regular expressions. the `r` on the front of the string just signifies it is a raw string literal, more on that here : http://stackoverflow.com/questions/2081640/what-exactly-do-u-and-rstring-flags-in-python-and-what-are-raw-string-litte – MadDogMcNamara Jul 08 '13 at 22:32
  • 1
    I would use the regular expression module if I were you :http://www.tutorialspoint.com/python/python_reg_expressions.htm – MadDogMcNamara Jul 08 '13 at 22:32
  • Also, I added a third dictionary with {' ',' '} and this time it got rid of all the double spaces. – CircuitB0T Jul 09 '13 at 16:28
  • when I run above code I get following error: > for i, j in replacements.iteritems(): > AttributeError: 'tuple' object has no attribute 'iteritems' – Yunus Hatipoglu Mar 27 '18 at 18:33