str.replace()将char替换为“ char”

时间:2018-07-12 14:53:25

标签: python-3.x python-3.6 str-replace

我有一个脚本,用于处理从Web API返回的数据。返回的数据是字典列表的字符串表示形式。

我知道ast.literal_eval()可以将字典的字符串表示形式转换为真实的形式。

数据看起来像这样(提醒:这些是字符串,而不是字典):

new = [{"id": "L0", "tags": null, "disable": false},
       {"id": "L1", "tags": null, "disable": false},
       {"id": "L2", "tags": null, "disable": false}]

因为'null'和'false'不在引号中,所以ast.literal_eval()失败并出现错误。如果我在上面的null和false处手动添加引号,则效果很好。

我试图通过这些字符串进行迭代,并将子字符串null和false修改为“ null”和“ false”,但这无济于事:

for i in new:
    i.replace("null", "\"null\"")
    i.replace("false", "\"false\"")

如何修改这些子字符串以添加引号?如果可能的话,我宁愿修改现有列表(new [])而不是创建一个新列表,但是如果我必须创建一个新列表才能使其正常工作,那我将这样做。

2 个答案:

答案 0 :(得分:1)

@MarkS ,如果 new 是词典列表的字符串表示形式,那么您也可以尝试以下代码。

from ast import literal_eval

new = '''[{"id": "L0", "tags": null, "disable": false},
       {"id": "L1", "tags": null, "disable": false},
       {"id": "L2", "tags": null, "disable": false}]'''

new = new.replace('false', '\"false\"')
new = new.replace('null', '\"null\"')

print(new)
"""
    [{"id": "L0", "tags": "null", "disable": "false"},
       {"id": "L1", "tags": "null", "disable": "false"},
       {"id": "L2", "tags": "null", "disable": "false"}]
"""

print(literal_eval(new))
"""
    [{'id': 'L0', 'tags': 'null', 'disable': 'false'}, {'id': 'L1', 'tags': 'null', 'disable': 'false'}, {'id': 'L2', 'tags': 'null', 'disable': 'false'}]
"""

答案 1 :(得分:0)

您可以在运行

205729.68706, 3,   0.464, -0.425,  9.903, 4,  -0.000,  0.001, -0.000, 5,  36.290,  4.707,-24.930205729.88997, 3,   0.443, -0.433,  9.885, 4,  -0.000,  0.001,  0.000205730.09288, 3,   0.465, -0.446,  9.894, 4,  -0.000, -0.000, -0.000, 5,  37.206,  5.412,-24.298205730.29576, 3,   0.468, -0.432,  9.897, 4,  -0.000, -0.000, -0.000, 5,  36.308,  5.411,-25.554205730.49867, 3,   0.442, -0.424,  9.883, 4,   0.000,  0.000, -0.000, 5,  36.868,  4.611,-26.465205730.70155, 3,   0.467, -0.444,  9.888, 4,  -0.000,  0.000, -0.000, 5,  37.498,  5.637,-25.841205730.90446, 3,   0.464, -0.435,  9.904, 4,  -0.001,  0.000, -0.000, 5,  36.301,  5.049,-24.931205731.10734, 3,   0.440, -0.432,  9.876, 4,  -0.000, -0.000, -0.000, 5,  36.308,  5.605,-26.483205731.31026, 3,   0.460, -0.440,  9.906, 4,  -0.000,  0.000,  0.000, 5,  35.706,  5.025,-24.944205731.51317, 3,   0.458, -0.434,  9.909, 4,   0.000,  0.000, -0.000, 5,  35.374,  4.218,-26.492
之前修改数据,可能使用205729.68706, 3, 0.464, -0.425, 9.903, 4, -0.000, 0.001, -0.000, 5, 36.290, 4.707,-24.930205729.88997, 3, 0.443, -0.433, 9.885, 4, -0.000, 0.001, 0.000205730.09288, 3, 0.465, -0.446, 9.894, 4, -0.000, -0.000, -0.000, 5, 37.206, 5.412,-24.298205730.29576, 3, 0.468, -0.432, 9.897, 4, -0.000, -0.000, -0.000, 5, 36.308, 5.411,-25.554205730.49867, 3, 0.442, -0.424, 9.883, 4, 0.000, 0.000, -0.000, 5, 36.868, 4.611,-26.465205730.70155, 3, 0.467, -0.444, 9.888, 4, -0.000, 0.000, -0.000, 5, 37.498, 5.637,-25.841205730.90446, 3, 0.464, -0.435, 9.904, 4, -0.001, 0.000, -0.000, 5, 36.301, 5.049,-24.931205731.10734, 3, 0.440, -0.432, 9.876, 4, -0.000, -0.000, -0.000, 5, 36.308, 5.605,-26.483205731.31026, 3, 0.460, -0.440, 9.906, 4, -0.000, 0.000, 0.000, 5, 35.706, 5.025,-24.944205731.51317, 3, 0.458, -0.434, 9.909, 4, 0.000, 0.000, -0.000, 5, 35.374, 4.218,-26.492 模块:

输出:

ast.literal_eval()