0
input = "[{'parentId': '3660050', 'id': '3813419', 'name': 'Diversity Report', 'parentName': 'Uber Crises'}, {'parentId': '3447845', 'id': '3447846', 'name': 'High', 'parentName': 'QA Intensity'}, {'parentId': '3446939', 'id': '3446940', 'name': 'High', 'parentName': 'Intensity'}, {'parentId': '3447834', 'id': '3447842', 'name': 'Disgust', 'parentName': 'QA Emotion'}, {'parentId': '3447822', 'id': '3447823', 'name': 'Negative', 'parentName': 'QA Sentiment'}, {'parentId': '3446760', 'id': '3446763', 'name': 'Neutral', 'parentName': 'Original Sentiment'}, {'parentId': '3446944', 'id': '3446947', 'name': 'Negative', 'parentName': 'Aggregated_Sentiment'}, {'parentId': '3446936', 'id': '3446938', 'name': 'Not Spam', 'parentName': 'Spam'}, {'parentId': '3446924', 'id': '3446932', 'name': 'Disgust', 'parentName': 'Emotion'}, {'parentId': '3720161', 'id': '3720163', 'name': 'Uber', 'parentName': 'Uber & Lyft'}, {'parentId': '3660050', 'id': '3723667', 'name': ""Waymo vs. Uber's Otto"", 'parentName': 'Uber Crises'}, {'parentId': '3660050', 'id': '3723665', 'name': 'Uber Sexism', 'parentName': 'Uber Crises'}, {'parentId': '3660050', 'id': '3660077', 'name': '#DeleteUber', 'parentName': 'Uber Crises'}]"

ast.literal_eval(input) or eval(input) still gives me a string type.

The goal is to get a list of dictionaries which I can iterate through.

iuppiter
  • 171
  • 1
  • 9

1 Answers1

0
input = """[{'parentId': '3660050', 'id': '3813419', 'name': 'Diversity Report', 'parentName': 'Uber Crises'}, {'parentId': '3447845', 'id': '3447846', 'name': 'High', 'parentName': 'QA Intensity'}, {'parentId': '3446939', 'id': '3446940', 'name': 'High', 'parentName': 'Intensity'}, {'parentId': '3447834', 'id': '3447842', 'name': 'Disgust', 'parentName': 'QA Emotion'}, {'parentId': '3447822', 'id': '3447823', 'name': 'Negative', 'parentName': 'QA Sentiment'}, {'parentId': '3446760', 'id': '3446763', 'name': 'Neutral', 'parentName': 'Original Sentiment'}, {'parentId': '3446944', 'id': '3446947', 'name': 'Negative', 'parentName': 'Aggregated_Sentiment'}, {'parentId': '3446936', 'id': '3446938', 'name': 'Not Spam', 'parentName': 'Spam'}, {'parentId': '3446924', 'id': '3446932', 'name': 'Disgust', 'parentName': 'Emotion'}, {'parentId': '3720161', 'id': '3720163', 'name': 'Uber', 'parentName': 'Uber & Lyft'}, {'parentId': '3660050', 'id': '3723667', 'name': "Waymo vs. Uber's Otto", 'parentName': 'Uber Crises'}, {'parentId': '3660050', 'id': '3723665', 'name': 'Uber Sexism', 'parentName': 'Uber Crises'}, {'parentId': '3660050', 'id': '3660077', 'name': '#DeleteUber', 'parentName': 'Uber Crises'}]"""

The above code should generate proper results under an eval call while preserving the apostrophe character in one of your strings. The error was specifically on the line with the apostrophe. For whatever reason it wouldn't evaluate correctly with an escape character, it likely tried to create a string. This solution works because instead of an escape character I just use a different type of string. Also, there should have only been one double quote around "Waymo vs. Uber's Otto" as shown. You probably thought that you could add the other ones because the string wasn't highlighting correctly, but the purpose of that was to tell you you made a mistake.

Matthew Ciaramitaro
  • 1,184
  • 1
  • 13
  • 27