0

I want to separate some words of a string but there is two types of divisions in the line using python: Example of the string:

add $s2, $s0, $s1

I want to separate in:

"add", "$s2", "$s0", "$s1"

but with data.split() function I can only Split them with "," or " ", but in this case between add and $s2 it doesn't have "," only " ".

arq = open('entrada.asm', 'r')
text = arq.readlines()
for line in text:
    (inst) = line.split(" ")
    (reg1, reg2, reg3) = line.split(",")
    print(inst, reg1, reg2, reg3)
arq.close()
nathancy
  • 42,661
  • 14
  • 115
  • 137
luscv
  • 1
  • 1
    Possible duplicate of [Split string with multiple delimiters in Python](https://stackoverflow.com/questions/4998629/split-string-with-multiple-delimiters-in-python) – busybear Apr 19 '19 at 01:55

1 Answers1

1

Use re.split:

import re
re.split('[ ,]+', 'add $s2, $s0, $s1')

Output:

['add', '$s2', '$s0', '$s1']
Chris
  • 29,127
  • 3
  • 28
  • 51