Search code examples
pythontextdata-cleaningtext-analysis

To seperate words outside brackets as well as inside brackets and put into separate columns in Python?


I need solution to the following string in my data set. Need to be splitted into various words to get meaningful insights.

a='(Barbecue)Cheese(earthyCamembert,Fontina,nuttyAsiago,Colby,Parmesan)General(Chocolate)Meat(Beef)

Here the first words (Barbecue) - represent cusine second word - Cheese(earthyCamembert,Fontina,nuttyAsiago,Colby,Parmesan) third word - General(Chocolate) fourth word - Meat(Beef)

Like this above example in need to split it into 4 categories. can anyone help me out to code it python. I am new to this. Thanks.


Solution

  • You could probably get what you need just using a.split(')'). This breaks the string up into a list at every ). You would end up with a being ['(Barbecue', 'Cheese(earthyCamembert,Fontina,nuttyAsiago,Colby,Parmesan'…] if that's what you're looking for. You could also fairly easily iterate through the list if you want that final parenthesis. If I had to guess, I'd say that what you want, however, is a dictionary.

    Barbecue = {'Cheese': ['earthyCamembert', 'Fontina', 'nuttyAsiago', 'Colby', 'Parmesan'],
                'General': ['Chocolate'],
                …}
    

    Also, being fairly new to Python and coding myself, I'd recommend checking out Codeacademy's introductory course to Python. It helped me out a lot. After completing it, I bet you could've solved this yourself.