Yelp - iterate thr spacing, comma but not working

hi,

my code below does not work, anyone knows why ?

data['categories']
[{'alias': 'mexican', 'title': 'Mexican'}, {'a...
# iterating through each record to get the title words, remove punctuation, and convert all words to lowercase. 
import ast
import re
data['tags'] = ''
for ix in data.index:
    # Evaluate categories as code
    cat = ast.literal_eval(data['categories'][ix])
    
    # Save tags as single string
    words = ''
    for tag in cat:
        words += f"{tag['title']} "
    
    # Remove punctuation
    words = re.sub(r'[^\w\s]', ' ', words)
data.loc[ix, 'tags'] = ' '.join([words.lower(), str(int(data.loc[ix, 'price']))])

OUTPUT below

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-90-0ec45374e90c> in <module>
      5 for ix in data.index:
      6     # Evaluate categories as code
----> 7     cat = ast.literal_eval(data['categories'][ix])
      8 
      9     # Save tags as single string

Why are you using the ast module for this? That’s for extremely low-level access to python source code itself, and almost certainly isn’t necessary here. What is it you’re trying to do? Also, please paste actually working code (the first two lines don’t look like real code)

the first 2 lines are just displaying what data[‘categories’] contains

I want Python to interpret this string as a list of dictionaries rather than read it as a string. i am using the [Abstract Syntax Trees](library and calling the literal_eval method, then saving the result as a variable. but it is not running successfully

Good on you for using the much safer ast.literal_eval instead of the builtin eval, but most programs should never need to use eval, and if they do, it should be considered a brutal hack of last resort.

Fun fact: simple python literals including dicts and lists are typically valid json. Have you tried just using the json library?