将字符串转换为Python中的字典列表

问题描述 投票:0回答:1

经常使用这个网站,但这是我的第一篇文章。 预先感谢任何能够提供建议的人!

我正在使用Python3读取Excel文件。 我有一堆单元格,其中包含看起来像 A) 字典或 B) 字典列表的数据。

这些在 Python 中作为字符串对象导入,我似乎无法将它们转换为字典或字典列表。 我尝试过 json.loads() 并不断收到错误

这是字符串对象中文本的示例:

[{'event': 'PUBLIC_ACCESS_CHANGED', 'timeOccurred': '2022-11-02T14:21:09.000-05:00'}, {'event': 'DISPOSITION_SUBMITTED', 'status': 'Approved as Amended', 'comments': '', 'timeOccurred': '2022-11-02T16:22:32.000-05:00'}, {'event': 'NOTE_SUBMITTED', 'subject': 'This is a subject line.', 'comments': 'User,\r\n\r\nThis is a note that was submitted.\r\n\r\nThanks,\r\nNote Author', 'timeOccurred': '2022-10-26T14:00:37.000-05:00', 'userSubmitted': 'Note Author'}]

我什至尝试用双引号替换单引号以使 json.loads() 工作,但似乎不起作用。

这是 json.loads() 错误:

---------------------------------------------------------------------------
JSONDecodeError                           Traceback (most recent call last)
<ipython-input-8-3162c498c23f> in <module>
     14     #print(endoffield)
     15     field_messages = templist[0]
---> 16     newfield = json.loads(field_messages)
     17 
     18     print('this is type(field_messages)', type(field_messages))

C:\ProgramData\Anaconda3\lib\json\__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    346             parse_int is None and parse_float is None and
    347             parse_constant is None and object_pairs_hook is None and not kw):
--> 348         return _default_decoder.decode(s)
    349     if cls is None:
    350         cls = JSONDecoder

    C:\ProgramData\Anaconda3\lib\json\decoder.py in decode(self, s, _w)
    335 
    336         """
--> 337         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    338         end = _w(s, end).end()
    339         if end != len(s):

C:\ProgramData\Anaconda3\lib\json\decoder.py in raw_decode(self, s, idx)
    353             obj, end = self.scan_once(s, idx)
    354         except StopIteration as err:
--> 355             raise JSONDecodeError("Expecting value", s, err.value) from None
    356         return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

这是我正在运行的代码:

for excelrow in ws_case.iter_rows(min_row=2, max_row=last_row, min_col=1, max_col=last_col, values_only=True):
    
    templist = list(excelrow[95:146]) # create temp list for current Excel row with just the fields I need
    field_casekey = excelrow[0] # grab the ID for the excel record 
    field_messages = templist[0] # load the field to a variable
    print('this is type(field_messages):', type(field_messages))
    print('this is field_messages:', field_messages)
    newfield = json.loads(field_messages)

这是抛出错误之前的输出:

this is type(field_messages): <class 'str'>

this is field_messages: [{'event': 'PUBLIC_ACCESS_CHANGED', 'timeOccurred': '2022-11-02T14:21:09.000-05:00'}, {'event': 'DISPOSITION_SUBMITTED', 'status': 'Approved as Amended', 'comments': '', 'timeOccurred': '2022-11-02T16:22:32.000-05:00'}, {'event': 'NOTE_SUBMITTED', 'subject': 'This is a subject line.', 'comments': 'User,\r\n\r\nThis is a note that was submitted.\r\n\r\nThanks,\r\nNote Author', 'timeOccurred': '2022-10-26T14:00:37.000-05:00', 'userSubmitted': 'Note Author'}]
python list dictionary
1个回答
0
投票

您实际上可以以编程方式执行@john-gordon 在上面的评论中提到的操作:

field_messages = (
    field_messages
        .replace("\'", "\"")
        .replace("\n", "\\n")
        .replace("\r", "\\r")
    )

这个解决方案并不是最佳的,因为你对字符串进行了 3 次迭代,而不是 1 次。我提供它只是为了展示这个概念。

最好使用正则表达式或手动执行,时间复杂度为 O(n)

© www.soinside.com 2019 - 2024. All rights reserved.