0

I have the following piece of code where I want to delete combinations like 1\n, 2\n, 3\n etc. from the string. It seems to me there should be a better way to exclude the combination rather than just enlist them all. Thanks in advance

instr_delete = ['\r', '1\n', '2\n', '3\n', '4\n', '5\n', '6\n', '7\n', '\n']
for bad_sign in instr_delete:
  if bad_sign in instructions:
    instructions = instructions.replace(bad_sign, '')
Alyona
  • 1
  • 1
  • Please be more specific in your question.. What should the final output look like? – ProteinGuy Jan 15 '22 at 09:36
  • As the input there is a string with instructions where steps are marked with 1\n, 2\n, 3\n etc. As the output I would like to see the string with the text without numbers denoting the steps and newline characters (and carriage return character as well) – Alyona Jan 15 '22 at 09:44
  • Does this answer your question? [Best way to replace multiple characters in a string?](https://stackoverflow.com/questions/3411771/best-way-to-replace-multiple-characters-in-a-string) – nikeros Jan 15 '22 at 09:54

1 Answers1

1

You can do this with the RE module:

import re

instructions = re.sub('[1-7]\n|\r|\n', '', instructions)
DarkKnight
  • 19,739
  • 3
  • 6
  • 22