【发布时间】:2017-10-03 10:14:53
【问题描述】:
我正在尝试解析带单引号的文件,并希望将其更改为双引号。
样本数据:
{'data': ['my',
'my_other',
'my_other',
'my_other',
'This could 'happen' <- and this ones i want to keep',
],
'name': 'MyName'},
'data2': {'type': 'x86_64',
'name': 'This',
'type': 'That',
'others': 'bla bla 'bla' <- keep this ones too',
'version': '21237821972'}}}
期望的输出:
{"data": ["my",
"my_other",
"my_other",
"my_other",
"This could 'happen' <- and this ones i want to keep"
],
"name": "MyName"},
"data2": {"type": "x86_64",
"name": "This",
"type": "That",
"others": "bla bla 'bla' <- keep this ones too",
"version": "21237821972"}}}
我已经尝试过用 sed 做一些正则表达式,但很不幸。 我明白为什么这对我不起作用,只是不知道如何进一步获取我想要的数据。
sed -E -e "s/( |\{|\[)'(.*)'(\:|,|\]|\})/\1\"\2\"\3/g"
干杯,
【问题讨论】:
-
您要解决的最终目标是什么?还有用python可以吗?
-
如果这是
JSON文件,推荐使用jq工具(或)使用python json模块 -
这是一些我想要转换为 json 的 python 字典和列表(带双引号)
-
有什么方法可以区分保留哪些引号和替换哪些引号,因为即使您采用保持句子中间引号完整的逻辑,我也会看到引号所在的行更改了您想要的输出。
-
不,我无法区分它们。对不起。