-1

I have a RegEx question that I'm having a hard time finding a solution. I want to process a list of strings of the type:

genre = '[{"id": 28, "name": "Action"}, {"id": 12, "name": "Adventure"}, {"id": 14, "name": "Fantasy"}, {"id": 878, "name": "Science Fiction"}]'

These are the "genre" column of a movies dataframe I am working with. I want to return a list of just the name of the genres. I've tried using re.match() and .search(). But I am too much of a RegEx beginner.

Could you help me with a solution for this?

Thank you so much in advance!

irenels
  • 3
  • 2

2 Answers2

1

You're asking about regular expressions, but what about doing something like this?

list_ = eval('[{"id": 28, "name": "Action"}, {"id": 12, "name": "Adventure"}, {"id": 14, "name": "Fantasy"}, {"id": 878, "name": "Science Fiction"}]')
[genre["name"] for genre in list_]

This is of course assuming that you only have the string. If you have the list itself, you don't need the workaround with eval. Also, watch out for security issues if your data is provided from an untrusted source.

myke
  • 479
  • 3
  • 14
  • Thank you for your reply :D I was not familiar with this method. The thing is this string is repeated over a column of a dataframe. It's not just one, I want to receive the differente genres for a set of movies. Many, many thanks. – irenels Feb 02 '20 at 19:56
  • You should post the exact way your data is available to you. – myke Feb 02 '20 at 19:57
  • You are right, sorry. I am newbie here. – irenels Feb 02 '20 at 20:00
0

This {"id": 12, "name": "Adventure"} are key-value pairs, the entire expression is a list of dictionaries, see Python list of dictionaries search, so Myke's answer is good, also see How to convert a string containing a list of dict into Python object?

ralf htp
  • 9,149
  • 4
  • 22
  • 34