mardi 17 septembre 2019

How to split a string with multiple conditions in python?

Suppose I have a string op= SU 3180 and (CMG 3200 or SU 3210) I would like an output like this ["SU", "3180"] ["CMG", "3200"] ["SU", "3210"]

My code looks like this:

if op.str.contains('None').item():
    print('No Prereq for this course :) ')

else: 
    string = list()
    if op.str.contains('or').item():
        string=op.str.split('or')
    if op.str.contains('and').item():
        string=op.str.split('and')

    for item in string:
        print("Pre-req number:",item)
        for i in item:
            res=i.split()
            print(res)

The ouput I am getting is like this ['SU', '3180'] ['(CMG', '3200', 'or', 'SU', '3210)']

How do I fix my code?

Aucun commentaire:

Enregistrer un commentaire