Format the raw data into the format that can be evaluated. Args: raw_data (str): The raw data. start_character (str, optional): The start character. Defaults to '', if using it, the string will be sliced from the first start_character. end_character (str, optional): The
(raw_data: str, start_character: str = '', end_character: str = '')
| 1 | import ast |
| 2 | import json |
| 3 | def format_load(raw_data: str, start_character: str = '', end_character: str = ''): |
| 4 | """Format the raw data into the format that can be evaluated. |
| 5 | |
| 6 | Args: |
| 7 | raw_data (str): The raw data. |
| 8 | start_character (str, optional): The start character. Defaults to '', if using it, the string will be sliced from the first start_character. |
| 9 | end_character (str, optional): The end character. Defaults to '', if using it, the string will be sliced to the last end_character. |
| 10 | |
| 11 | Returns: |
| 12 | str: The formatted data. |
| 13 | """ |
| 14 | if type(raw_data) != str: |
| 15 | # the data has been evaluated |
| 16 | return raw_data |
| 17 | if "```json" in raw_data: |
| 18 | raw_data = raw_data[raw_data.find("```json") + len("```json"):] |
| 19 | raw_data = raw_data.strip("`") |
| 20 | if start_character != '': |
| 21 | raw_data = raw_data[raw_data.find(start_character):] |
| 22 | if end_character != '': |
| 23 | raw_data = raw_data[:raw_data.rfind(end_character) + len(end_character)] |
| 24 | successful_parse = False |
| 25 | try: |
| 26 | data = ast.literal_eval(raw_data) |
| 27 | successful_parse = True |
| 28 | except Exception as e: |
| 29 | pass |
| 30 | try: |
| 31 | if not successful_parse: |
| 32 | data = json.loads(raw_data) |
| 33 | successful_parse = True |
| 34 | except Exception as e: |
| 35 | pass |
| 36 | try: |
| 37 | if not successful_parse: |
| 38 | data = json.loads(raw_data.replace("\'", "\"")) |
| 39 | successful_parse = True |
| 40 | except Exception as e: |
| 41 | pass |
| 42 | if not successful_parse: |
| 43 | raise Exception("Cannot parse raw data") |
| 44 | return data |
no test coverage detected