如何在python中将字符串转换为json?

时间:2014-06-26 23:29:58

标签: python json

我有一个字符串,当我这样做时,我从git获得

> git show xxxx | head -3

commit 34343asdfasdf343434asdfasdfas
Author: John Doe <john@doe.com>
Date:   Wed Jun 25 09:51:49 2014 +0800

我需要使用python将打印到控制台的这个字符串转换为json格式,这样格式就是(我在脚本中写这个)

{'commit': '34343asdfasd343adfas', 'Author': 'john doe', 'date': 'wed jun 25'} 

目前我正在尝试按字符串中的第一个空格手动分割。

3 个答案:

答案 0 :(得分:3)

这适用于您的示例:

>>> txt='''\
... commit 34343asdfasdf343434asdfasdfas
... Author: John Doe <john@doe.com>
... Date:   Wed Jun 25 09:51:49 2014 +0800'''
>>> json.dumps({k:v for k,v in re.findall(r'^([^\s]+)\s+(.+?)$', txt, re.M)})
{"commit": "34343asdfasdf343434asdfasdfas", "Date:": "Wed Jun 25 09:51:49 2014 +0800", "Author:": "John Doe <john@doe.com>"}

如果您有git...部分,请将其拆分:

>>> json.dumps({k:v for k,v in re.findall(r'^([^\s]+)\s+(.+?)$', 
                           txt.partition('\n\n')[2], re.M)})

如果您想要松开:,只需更改正则表达式捕获组即可:

>>> json.dumps({k:v for k,v in re.findall(r'^(\w+):?\s+(.+?)$', 
                          txt.partition('\n\n')[2], re.M)})
{"Date": "Wed Jun 25 09:51:49 2014 +0800", "commit": "34343asdfasdf343434asdfasdfas", "Author": "John Doe <john@doe.com>"}

如果您想要丢失电子邮件地址:

>>> json.dumps({k:v for k,v in re.findall(r'^(\w+):?\s+(.+?)(?:\s*<[^>]*>)?$', 
                     txt.partition('\n\n')[2], re.M)})
{"Date": "Wed Jun 25 09:51:49 2014 +0800", 
 "commit": "34343asdfasdf343434asdfasdfas", "Author": "John Doe"}

答案 1 :(得分:0)

尝试遵循确切的值格式

OP要求提供一些自定义值,这些值不是删除标签字后留下的确切值。为此,我们必须逐行解决。

获取文本输入并将其拆分为行:

>>> text = """commit 34343asdfasdf343434asdfasdfas
... Author: John Doe <john@doe.com>
... Date:   Wed Jun 25 09:51:49 2014 +0800"""
...
>>> lines = text.split("\n")
>>> lines
['commit 34343asdfasdf343434asdfasdfas',
 'Author: John Doe <john@doe.com>',
 'Date:   Wed Jun 25 09:51:49 2014 +0800']

假设线的固定顺序和部分处理:

>>> dct = {}
>>> dct["commit"] = lines[0].split()[1]
>>> dct["Author"] = lines[1].split(": ")[1].split(" <")[0]
>>> dct["Date"] = " ".join(lines[2].split(": ")[1].split()[:3])

创建最终字典:

>>> dct
{'Author': 'John Doe',
 'Date': 'Wed Jun 25',
 'commit': '34343asdfasdf343434asdfasdfas'}

可以转储为字符串:

>>> import json
>>> json.dumps(dct)
'{"Date": "Wed Jun 25", "commit": "34343asdfasdf343434asdfasdfas", "Author": "John Doe"}'

答案 2 :(得分:0)

import subprocess
results = subproccess.Popen("git show xxxx | head -3",stdout=subprocess.PIPE,shell=True).communicate()[0].strip()
data = {}
for line in results.splitlines():
     key,value = line.split(" ",1)
     data[re.sub("[^a-zA-Z]","",key)] = value
json.dumps(data)