1

I am trying to replace the µ with micro in a string. It could replace some strings but it does not perform for some of them, for example, it changes 20.0+-1.8% hemolysis at 50µM to 20.0+-1.8% hemolysis at 50microM but it does not change it in this case: 0% hemolysis at 312 to 0μg/ml. What could be the reason?

rep = {"±": "+-", "µ": "micro"}
rep = dict((re.escape(k), v) for k, v in rep.items())
pattern = re.compile("|".join(rep.keys()))
activity = pattern.sub(lambda m: rep[re.escape(m.group(0))], rows[i + 1].string.strip())

Even the replace() did not change it:

activity = rows[i + 1].string.strip().replace('±','+-').replace('µ': 'micro')

Solution: The comment by @user2357112 could help to find a simple solution:

I changed the dictionary to this:

rep = {"±": "  +-  ", "µ": "micro", '\u03BC' : 'micro'}
S.EB
  • 1,966
  • 4
  • 29
  • 54
  • 1
    `replace('μ': 'micro')`? Is the colon a typo? – Mechanic Pig Oct 05 '22 at 01:56
  • It looks like you could skip a few steps using [maketrans](https://docs.python.org/3/library/stdtypes.html?highlight=maketrans#str.maketrans) and [translate](https://docs.python.org/3/library/stdtypes.html?highlight=maketrans#str.translate)? – 0x263A Oct 05 '22 at 01:58
  • 5
    One of those characters is U+00B5 MICRO SIGN. The other is U+03BC GREEK SMALL LETTER MU. – user2357112 Oct 05 '22 at 01:59
  • [Here's this fwiw](https://tio.run/##RU5LCsIwFNznFI@I2GCtn25E8DBpE9tgk5TkIRTxUC7cddUD9EoxKYKbYQbm1w/YWlOeexeCqBGu8KTTm16Abnc0Bzp9EteqdjbJefzLF8GqiwGPrtD8LtFx47NYwghZAbbKNBE5glDCbBDqlptGEifTCj2soZXadoNXHqKpPJ4ALRzmsdnrjhZLW8dRZnGFkd4pg1nMLuU35Tzm4GVtjQBe2Yf8OawTWTrNclhoPMwYCeEL) – 0x263A Oct 05 '22 at 02:06
  • @user2357112 thanks, how to change my code then? – S.EB Oct 05 '22 at 03:08
  • Change it to look for both variants, or separately apply Unicode normalization before you try to match. – tripleee Oct 05 '22 at 04:47

0 Answers0