abn
abn

Reputation: 1373

Replace items in a CSV file Python using a list

I have a list like below:

['data-data analysis','word or words-phrase','rank-ranking']

and a regular CSV file which could contain the word before "-" anywhere in it (any column). I'd like to replace those with the words after "-". A sample CSV file could be like below:

h1,h2,h3
data of database,a,v
gg,word or words/word,gdg
asd,r,rank

I really appreciate any help.

Desired output:

h1,h2,h3
data analysis of database,a,v
gg,phrase/word,gdg
asd,r,ranking

Upvotes: 0

Views: 1554

Answers (1)

Mark Tolonen
Mark Tolonen

Reputation: 178179

This has some trickery so you don't get data analysis of data analysisbase when replacing data:

input.csv

h1,h2,h3
data of database,a,v
gg,word or words/word,gdg
asd,r,rank

Python code

#!python2
import csv
import re

# This builds a dictionary of key/value replacements.
# It wraps the key in word breaks to handle not replacing
# "database" when the key is "data".
L = ['data-data analysis','word or words-phrase','rank-ranking']
pairs = [w.split('-') for w in L]
replacements = {r'\b' + re.escape(k) + r'\b':v for k,v in pairs}

# Files should be opened in binary mode for use with csv module.
with open('input.csv','rb') as inp:
    with open('output.csv','wb') as outp:

        # wrap the file streams in csv reader and csv writer objects.
        r = csv.reader(inp)
        w = csv.writer(outp)

        for line in r:
            for i,item in enumerate(line):
                for k,v in replacements.items():
                    item = re.sub(k,v,item)
                line[i] = item
            w.writerow(line)

output.csv

h1,h2,h3
data analysis of database,a,v
gg,phrase/word,gdg
asd,r,ranking

Upvotes: 1

Related Questions