Search code examples
pythoncsvreplacegeneratorgenerator-expression

Using multiple (similar) generator expressions


In a csv file, I'm trying to replace certain characters with other characters.

My current code is similar to this:

import csv

set1 = set('abc')
set2 = set('def')
set3 = set('ghi')

with open(path, 'r') as input, open(path2, 'w') as output:
    reader = csv.reader(input)
    writer = csv.writer(output)

    for row in reader:
            newrow = row
            newrow = [''.join('x' if c in set1 else c for c in item) for item in newrow]
            newrow = [''.join('y' if c in set2 else c for c in item) for item in newrow]
            newrow = [''.join('z' if c in set3 else c for c in item) for item in newrow]

            writer.writerow(newrow)

In this example I am only using three generator expressions, but it could easily be more than that.

Does anyone know the right way to do this? My concern is that this structure might not be the fastest (and certainly doesn't look optimal).


Solution

  • str.translate might be appropriate; something along the lines of

    replacements = [
        ('abc', 'x'),
        ('def', 'y'),
        ('ghi', 'z'),
    ]
    
    trans = str.maketrans({ k: v for l, v in replacements for k in l })
    

    and

    new_row = [item.translate(trans) for item in row]