0

I want to replace multiple strings with a single string in a list. Reference: Find and replace string values in list

cols = ['M23group','M23met_event', 'M23mode', 'M23validation_1min', 'M23validation_2min', 'M23voltage',
 'M24AmbientAir_Av', 'M24Efficiency', 'M24FF', 'M24Impp', 'M24Isc',]
  cols = [w.replace('M23'|'M24', 'M') for w in cols]

Present output:

TypeError: unsupported operand type(s) for |: 'str' and 'str'
Mainland
  • 4,110
  • 3
  • 25
  • 56

2 Answers2

1

string replace() method takes 1 source and 1 destination, try this:

w.replace("M23", 'M').replace("M24", 'M')

Line should be like below:

cols = [w.replace("M23", 'M').replace("M24", 'M') for w in cols]
1

cols input - ['M23group', 'M23met_event', 'M23mode', 'M23validation_1min', 'M23validation_2min', 'M23voltage', 'M24AmbientAir_Av', 'M24Efficiency', 'M24FF', 'M24Impp', 'M24Isc']

cols = [w.replace('M23', 'M').replace('M24', 'M') for w in cols]

cols output - ['Mgroup', 'Mmet_event', 'Mmode', 'Mvalidation_1min', 'Mvalidation_2min', 'Mvoltage', 'MAmbientAir_Av', 'MEfficiency', 'MFF', 'MImpp', 'MIsc']

Prethi G
  • 54
  • 3