将随机字符序列转换为JSON

时间:2018-02-25 02:12:29

标签: python json regex string

我有一个很长的列表(或任何你可能称之为)的后续字符串格式:

“(str10,str20,str30),(str11,str21,str31),(str12,str22,str32),...”

括号内的所有字符串都是单引号。

我想将整个字符串转换为JSON,将括号内的所有字符串作为值提取并为其分配一定的键。因为我只知道JS和Python(我显然不是专业人士),所以我尝试使用正则表达式。但是,问题是 JS不能将上面的列表(或者你可能称之为的任何内容)识别为字符串。手动添加转义字符是不切实际的,因为列表很长。所以,我无法弄清楚如何做到这一点......

是否有一种通用方法(可能是适合的语言,某个库或某种方法)来搜索这些模式并将其提取出来?谢谢!

我的尝试

/([A-Z]{2})',\s'(.+)',\s'(.+)/g

示例列表

"""
('AD', 'Andorra', 'Hola'),
('AE', 'United Arab Emirates', 'Marhaba'),
('AF', 'Afghanistan', 'Senga yai'),
('AG', 'Antigua and Barbuda', 'Hello'),
('AI', 'Anguilla', 'Hello'),
('AL', 'Albania', 'Tungjatjeta'), ...
"""

预期输出

[{"key1": "AD", "key2": "Andorra", "key3": "Hola"},
  {"key1": "AE", "key2": "United Arab Emirates", "key3": "Marhaba"},
  {"key1": "AF", "key2": "Afghanistan", "key3": "Senga yai"},
  {"key1": "AG", "key2": "Antigua and Barbuda", "key3": "Hello"},
  {"key1": "AI", "key2": "Anguilla", "key3": "Hello"},
  {"key1": "AL", "key2": "Albania", "key3": "Tungjatjeta"}, ...
]

1 个答案:

答案 0 :(得分:1)

使用re.sub()和正则表达式。

正则表达式\(\s*'([^']+)'[^']+'([^']+)'[^']+'([^']+)'\s*\)

替换:{"key1": "\1", "key2": "\2", "key3": "\3"}

Python代码

r = r"\(\s*'([^']+)'[^']+'([^']+)'[^']+'([^']+)'\s*\)"
sub = r'{"key1": "\1", "key2": "\2", "key3": "\3"}'

json = "[%s]" % re.sub(r, sub, text)

输出:

[{"key1": "str10", "key2": "str20", "key3": "str30"}, {"key1": "str11", "key2": "str21", "key3": "str31"}, {"key1": "str11", "key2": "str21", "key3": "str31"}]

Code demo