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)

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

10 Comments

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
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?
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".
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 : stackoverflow.com/questions/2081640/…
I would use the regular expression module if I were you :tutorialspoint.com/python/python_reg_expressions.htm
|

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.