Не удается преобразовать строки в словари / JSON - PullRequest
0 голосов
/ 06 ноября 2018

Я читаю файл Excel в фрейме данных Pandas.

В столбце "plot_ehaps" есть строковые представления списка словарей, которые я хочу превратить в словари, подобные этому:

for cell in range(len(df)):
    d = ast.literal_eval(df['segment_efforts'].values[cell]

Однако я получаю

EOL при сканировании строкового литерала

literal_eval будет нормально работать, однако, для одной ячейки, скопированной в редактор и с тройными кавычками.

Как исправить приведенный выше код для достижения того же эффекта, что и в тройных кавычках вокруг строки?

Пример строки:

[{'elapsed_time': 628, 'device_watts': True, 'activity': {'resource_state': 1, 'id': 1462917682}, 'lap_index': 1, 'id': 4679807677, 'average_watts': 143.0, 'average_speed': 7.5, 'average_cadence': 114.4, 'start_date_local': '2018-03-20T19:45:40Z', 'distance': 4708.72, 'split': 1, 'start_index': 0, 'name': 'Lap 1', 'max_speed': 8.8, 'average_heartrate': 126.3, 'end_index': 628, 'moving_time': 628, 'start_date': '2018-03-20T18:45:40Z', 'max_heartrate': 143.0, 'resource_state': 2, 'athlete': {'resource_state': 1, 'id': 3255732}, 'total_elevation_gain': 0.0}, {'elapsed_time': 309, 'device_watts': True, 'activity': {'resource_state': 1, 'id': 1462917682}, 'lap_index': 2, 'id': 4679807680, 'average_watts': 209.2, 'average_speed': 8.09, 'average_cadence': 119.2, 'start_date_local': '2018-03-20T19:56:09Z', 'distance': 2499.13, 'split': 2, 'start_index': 629, 'name': 'Lap 2', 'max_speed': 8.3, 'average_heartrate': 150.6, 'end_index': 937, 'moving_time': 309, 'start_date': '2018-03-20T18:56:09Z', 'max_heartrate': 155.0, 'resource_state': 2, 'athlete': {'resource_state': 1, 'id': 3255732}, 'total_elevation_gain': 0.0}, {'elapsed_time': 96, 'device_watts': True, 'activity': {'resource_state': 1, 'id': 1462917682}, 'lap_index': 3, 'id': 4679807683, 'average_watts': 137.2, 'average_speed': 5.03, 'average_cadence': 84.6, 'start_date_local': '2018-03-20T20:01:18Z', 'distance': 482.84, 'split': 3, 'start_index': 938, 'name': 'Lap 3', 'max_speed': 7.5, 'average_heartrate': 131.3, 'end_index': 1034, 'moving_time': 96, 'start_date': '2018-03-20T19:01:18Z', 'max_heartrate': 151.0, 'resource_state': 2, 'athlete': {'resource_state': 1, 'id': 3255732}, 'total_elevation_gain': 0.0}, {'elapsed_time': 306, 'device_watts': True, 'activity': {'resource_state': 1, 'id': 1462917682}, 'lap_index': 4, 'id': 4679807685, 'average_watts': 209.3, 'average_speed': 8.06, 'average_cadence': 119.1, 'start_date_local': '2018-03-20T20:02:55Z', 'distance': 2467.17, 'split': 4, 'start_index': 1035, 'name': 'Lap 4', 'max_speed': 8.3, 'average_heartrate': 148.6, 'end_index': 1340, 'moving_time': 306, 'start_date': '2018-03-20T19:02:55Z', 'max_heartrate': 157.0, 'resource_state': 2, 'athlete': {'resource_state': 1, 'id': 3255732}, 'total_elevation_gain': 0.0}, {'elapsed_time': 94, 'device_watts': True, 'activity': {'resource_state': 1, 'id': 1462917682}, 'lap_index': 5, 'id': 4679807688, 'average_watts': 149.6, 'average_speed': 6.09, 'average_cadence': 91.5, 'start_date_local': '2018-03-20T20:08:01Z', 'distance': 572.04, 'split': 5, 'start_index': 1341, 'name': 'Lap 5', 'max_speed': 8.1, 'average_heartrate': 136.6, 'end_index': 1435, 'moving_time': 94, 'start_date': '2018-03-20T19:08:01Z', 'max_heartrate': 147.0, 'resource_state': 2, 'athlete': {'resource_state': 1, 'id': 3255732}, 'total_elevation_gain': 0.0}, {'elapsed_time': 323, 'device_watts': True, 'activity': {'resource_state': 1, 'id': 1462917682}, 'lap_index': 6, 'id': 4679807691, 'average_watts': 209.0, 'average_speed': 8.09, 'average_cadence': 119.1, 'start_date_local': '2018-03-20T20:09:36Z', 'distance': 2612.63, 'split': 6, 'start_index': 1436, 'name': 'Lap 6', 'max_speed': 8.3, 'average_heartrate': 148.9, 'end_index': 1759, 'moving_time': 323, 'start_date': '2018-03-20T19:09:36Z', 'max_heartrate': 157.0, 'resource_state': 2, 'athlete': {'resource_state': 1, 'id': 3255732}, 'total_elevation_gain': 0.0}, {'elapsed_time': 103, 'device_watts': True, 'activity': {'resource_state': 1, 'id': 1462917682}, 'lap_index': 7, 'id': 4679807693, 'average_watts': 145.2, 'average_speed': 5.97, 'average_cadence': 91.5, 'start_date_local': '2018-03-20T20:15:00Z', 'distance': 614.71, 'split': 7, 'start_index': 1760, 'name': 'Lap 7', 'max_speed': 7.7, 'average_heartrate': 134.1, 'end_index': 1862, 'moving_time': 103, 'start_date': '2018-03-20T19:15:00Z', 'max_heartrate': 153.0, 'resource_state': 2, 'athlete': {'resource_state': 1, 'id': 3255732}, 'total_elevation_gain': 0.0}, {'elapsed_time': 308, 'device_watts': True, 'activity': {'resource_state': 1, 'id': 1462917682}, 'lap_index': 8, 'id': 4679807695, 'average_watts': 209.0, 'average_speed': 7.97, 'average_cadence': 117.9, 'start_date_local': '2018-03-20T20:16:43Z', 'distance': 2454.1, 'split': 8, 'start_index': 1863, 'name': 'Lap 8', 'max_speed': 8.3, 'average_heartrate': 147.7, 'end_index': 2170, 'moving_time': 308, 'start_date': '2018-03-20T19:16:43Z', 'max_heartrate': 154.0, 'resource_state': 2, 'athlete': {'resource_state': 1, 'id': 3255732}, 'total_elevation_gain': 0.0}, {'elapsed_time': 96, 'device_watts': True, 'activity': {'resource_state': 1, 'id': 1462917682}, 'lap_index': 9, 'id': 4679807697, 'average_watts': 142.5, 'average_speed': 5.25, 'average_cadence': 80.9, 'start_date_local': '2018-03-20T20:21:51Z', 'distance': 503.87, 'split': 9, 'start_index': 2171, 'name': 'Lap 9', 'max_speed': 7.8, 'average_heartrate': 133.8, 'end_index': 2266, 'moving_time': 96, 'start_date': '2018-03-20T19:21:51Z', 'max_heartrate': 152.0, 'resource_state': 2, 'athlete': {'resource_state': 1, 'id': 3255732}, 'total_elevation_gain': 0.0}, {'elapsed_time': 306, 'device_watts': True, 'activity': {'resource_state': 1, 'id': 1462917682}, 'lap_index': 10, 'id': 4679807701, 'average_watts': 218.6, 'average_speed': 8.14, 'average_cadence': 120.0, 'start_date_local': '2018-03-20T20:23:27Z', 'distance': 2491.13, 'split': 10, 'start_index': 2267, 'name': 'Lap 10', 'max_speed': 8.4, 'average_heartrate': 151.5, 'end_index': 2573, 'moving_time': 306, 'start_date': '2018-03-20T19:23:27Z', 'max_heartrate': 158.0, 'resource_state': 2, 'athlete': {'resource_state': 1, 'id': 3255732}, 'total_elevation_gain': 0.0}, {'elapsed_time': 265, 'device_watts': True, 'activity': {'resource_state': 1, 'id': 1462917682}, 'lap_index': 11, 'id': 4679807703, 'average_watts': 115.7, 'average_speed': 6.56, 'average_cadence': 102.1, 'start_date_local': '2018-03-20T20:28:34Z', 'distance': 1739.46, 'split': 11, 'start_index': 2574, 'name': 'Lap 11', 'max_speed': 7.9, 'average_heartrate': 125.3, 'end_index': 2839, 'moving_time': 265, 'start_date': '2018-03-20T19:28:34Z', 'max_heartrate': 154.0, 'resource_state': 2, 'athlete': {'resource_state': 1, 'id': 3255732}, 'total_elevation_gain': 0.0}]

1 Ответ

0 голосов
/ 07 ноября 2018

Я предполагаю, что ваши данные исходили из Python напрямую, без явного преобразования в формат JSON, поэтому вы получаете несколько ошибок при попытке проанализировать строку примера как JSON.

Эти проблемы:

  1. Использование одинарных кавычек вместо двойных
  2. Использование Python True вместо JavaScript true
  3. Использование Python False вместо JavaScript false
  4. Использование Python None вместо JavaScript null

Если вы замените эти четыре проблемных стиля в своей строке, можно проанализировать как JSON, не поднимая JSONDecodeError.

Пример:

import json


s = """[{'elapsed_time': 628, 'device_watts': True, 'activity': {'resource_state': 1, 'id': 1462917682}, 'lap_index': 1, 'id': 4679807677, 'average_watts': 143.0, 'average_speed': 7.5, 'average_cadence': 114.4, 'start_date_local': '2018-03-20T19:45:40Z', 'distance': 4708.72, 'split': 1, 'start_index': 0, 'name': 'Lap 1', 'max_speed': 8.8, 'average_heartrate': 126.3, 'end_index': 628, 'moving_time': 628, 'start_date': '2018-03-20T18:45:40Z', 'max_heartrate': 143.0, 'resource_state': 2, 'athlete': {'resource_state': 1, 'id': 3255732}, 'total_elevation_gain': 0.0}, {'elapsed_time': 309, 'device_watts': True, 'activity': {'resource_state': 1, 'id': 1462917682}, 'lap_index': 2, 'id': 4679807680, 'average_watts': 209.2, 'average_speed': 8.09, 'average_cadence': 119.2, 'start_date_local': '2018-03-20T19:56:09Z', 'distance': 2499.13, 'split': 2, 'start_index': 629, 'name': 'Lap 2', 'max_speed': 8.3, 'average_heartrate': 150.6, 'end_index': 937, 'moving_time': 309, 'start_date': '2018-03-20T18:56:09Z', 'max_heartrate': 155.0, 'resource_state': 2, 'athlete': {'resource_state': 1, 'id': 3255732}, 'total_elevation_gain': 0.0}, {'elapsed_time': 96, 'device_watts': True, 'activity': {'resource_state': 1, 'id': 1462917682}, 'lap_index': 3, 'id': 4679807683, 'average_watts': 137.2, 'average_speed': 5.03, 'average_cadence': 84.6, 'start_date_local': '2018-03-20T20:01:18Z', 'distance': 482.84, 'split': 3, 'start_index': 938, 'name': 'Lap 3', 'max_speed': 7.5, 'average_heartrate': 131.3, 'end_index': 1034, 'moving_time': 96, 'start_date': '2018-03-20T19:01:18Z', 'max_heartrate': 151.0, 'resource_state': 2, 'athlete': {'resource_state': 1, 'id': 3255732}, 'total_elevation_gain': 0.0}, {'elapsed_time': 306, 'device_watts': True, 'activity': {'resource_state': 1, 'id': 1462917682}, 'lap_index': 4, 'id': 4679807685, 'average_watts': 209.3, 'average_speed': 8.06, 'average_cadence': 119.1, 'start_date_local': '2018-03-20T20:02:55Z', 'distance': 2467.17, 'split': 4, 'start_index': 1035, 'name': 'Lap 4', 'max_speed': 8.3, 'average_heartrate': 148.6, 'end_index': 1340, 'moving_time': 306, 'start_date': '2018-03-20T19:02:55Z', 'max_heartrate': 157.0, 'resource_state': 2, 'athlete': {'resource_state': 1, 'id': 3255732}, 'total_elevation_gain': 0.0}, {'elapsed_time': 94, 'device_watts': True, 'activity': {'resource_state': 1, 'id': 1462917682}, 'lap_index': 5, 'id': 4679807688, 'average_watts': 149.6, 'average_speed': 6.09, 'average_cadence': 91.5, 'start_date_local': '2018-03-20T20:08:01Z', 'distance': 572.04, 'split': 5, 'start_index': 1341, 'name': 'Lap 5', 'max_speed': 8.1, 'average_heartrate': 136.6, 'end_index': 1435, 'moving_time': 94, 'start_date': '2018-03-20T19:08:01Z', 'max_heartrate': 147.0, 'resource_state': 2, 'athlete': {'resource_state': 1, 'id': 3255732}, 'total_elevation_gain': 0.0}, {'elapsed_time': 323, 'device_watts': True, 'activity': {'resource_state': 1, 'id': 1462917682}, 'lap_index': 6, 'id': 4679807691, 'average_watts': 209.0, 'average_speed': 8.09, 'average_cadence': 119.1, 'start_date_local': '2018-03-20T20:09:36Z', 'distance': 2612.63, 'split': 6, 'start_index': 1436, 'name': 'Lap 6', 'max_speed': 8.3, 'average_heartrate': 148.9, 'end_index': 1759, 'moving_time': 323, 'start_date': '2018-03-20T19:09:36Z', 'max_heartrate': 157.0, 'resource_state': 2, 'athlete': {'resource_state': 1, 'id': 3255732}, 'total_elevation_gain': 0.0}, {'elapsed_time': 103, 'device_watts': True, 'activity': {'resource_state': 1, 'id': 1462917682}, 'lap_index': 7, 'id': 4679807693, 'average_watts': 145.2, 'average_speed': 5.97, 'average_cadence': 91.5, 'start_date_local': '2018-03-20T20:15:00Z', 'distance': 614.71, 'split': 7, 'start_index': 1760, 'name': 'Lap 7', 'max_speed': 7.7, 'average_heartrate': 134.1, 'end_index': 1862, 'moving_time': 103, 'start_date': '2018-03-20T19:15:00Z', 'max_heartrate': 153.0, 'resource_state': 2, 'athlete': {'resource_state': 1, 'id': 3255732}, 'total_elevation_gain': 0.0}, {'elapsed_time': 308, 'device_watts': True, 'activity': {'resource_state': 1, 'id': 1462917682}, 'lap_index': 8, 'id': 4679807695, 'average_watts': 209.0, 'average_speed': 7.97, 'average_cadence': 117.9, 'start_date_local': '2018-03-20T20:16:43Z', 'distance': 2454.1, 'split': 8, 'start_index': 1863, 'name': 'Lap 8', 'max_speed': 8.3, 'average_heartrate': 147.7, 'end_index': 2170, 'moving_time': 308, 'start_date': '2018-03-20T19:16:43Z', 'max_heartrate': 154.0, 'resource_state': 2, 'athlete': {'resource_state': 1, 'id': 3255732}, 'total_elevation_gain': 0.0}, {'elapsed_time': 96, 'device_watts': True, 'activity': {'resource_state': 1, 'id': 1462917682}, 'lap_index': 9, 'id': 4679807697, 'average_watts': 142.5, 'average_speed': 5.25, 'average_cadence': 80.9, 'start_date_local': '2018-03-20T20:21:51Z', 'distance': 503.87, 'split': 9, 'start_index': 2171, 'name': 'Lap 9', 'max_speed': 7.8, 'average_heartrate': 133.8, 'end_index': 2266, 'moving_time': 96, 'start_date': '2018-03-20T19:21:51Z', 'max_heartrate': 152.0, 'resource_state': 2, 'athlete': {'resource_state': 1, 'id': 3255732}, 'total_elevation_gain': 0.0}, {'elapsed_time': 306, 'device_watts': True, 'activity': {'resource_state': 1, 'id': 1462917682}, 'lap_index': 10, 'id': 4679807701, 'average_watts': 218.6, 'average_speed': 8.14, 'average_cadence': 120.0, 'start_date_local': '2018-03-20T20:23:27Z', 'distance': 2491.13, 'split': 10, 'start_index': 2267, 'name': 'Lap 10', 'max_speed': 8.4, 'average_heartrate': 151.5, 'end_index': 2573, 'moving_time': 306, 'start_date': '2018-03-20T19:23:27Z', 'max_heartrate': 158.0, 'resource_state': 2, 'athlete': {'resource_state': 1, 'id': 3255732}, 'total_elevation_gain': 0.0}, {'elapsed_time': 265, 'device_watts': True, 'activity': {'resource_state': 1, 'id': 1462917682}, 'lap_index': 11, 'id': 4679807703, 'average_watts': 115.7, 'average_speed': 6.56, 'average_cadence': 102.1, 'start_date_local': '2018-03-20T20:28:34Z', 'distance': 1739.46, 'split': 11, 'start_index': 2574, 'name': 'Lap 11', 'max_speed': 7.9, 'average_heartrate': 125.3, 'end_index': 2839, 'moving_time': 265, 'start_date': '2018-03-20T19:28:34Z', 'max_heartrate': 154.0, 'resource_state': 2, 'athlete': {'resource_state': 1, 'id': 3255732}, 'total_elevation_gain': 0.0}]"""

s = s.replace("'", '"')
s = s.replace("True", "true")
s = s.replace("False", "false")
s = s.replace("None", "null")

d = json.loads(s)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...