使用列表替换 CSV 文件 Python 中的项目

Replace items in a CSV file Python using a list

我有如下列表:

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

和一个常规的 CSV 文件,可以在其中的任何位置(任何列)包含“-”之前的单词。我想用“-”之后的词替换那些。示例 CSV 文件可能如下所示:

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

非常感谢任何帮助。

期望的输出:

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

这有一些技巧,所以当替换 data:

时你不会得到 data analysis of data analysisbase

input.csv

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

Python代码

#!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