【问题标题】:Python - regex to get dynamic usernames from JSON textPython - 正则表达式从 JSON 文本中获取动态用户名
【发布时间】:2018-02-26 21:58:26
【问题描述】:

我正在尝试从 JSON 转储中提取“登录”的值,该转储采用文本 (response.text) 形式

这是字符串:

{
   "name":"master",
   "commit":{
      "sha":"adc3208a9ac76262250a",
      "commit":{
         "author":{
            "name":"root",
            "email":"dan.ja@foo.ca",
            "date":"2018-02-26T20:14:41Z"
         },
         "committer":{
            "name":"GitHub Enterprise",
            "date":"2018-02-26T20:14:41Z"
         },
         "message":"Update README.md",
         "tree":{
            "sha":"3e4710d0e021a0a7",
            "comment_count":0,
            "verification":{
               "verified":false,
               "reason":"unsigned",
               "signature":null,
               "payload":null
            }
         },
         "author":{
            "login":"kyle",
            "id":5
         }

我只是想从最后一行的登录中提取值“kyle”。 'kyle' 的值可以改变,因为它每次都可以是不同的登录名。因此我需要 "login":"string"

中的字符串

这就是我现在所拥有的,但这只会让我“登录”:

/"login"[^\a]*"/g

【问题讨论】:

  • 你为什么要使用正则表达式呢?为什么不用data = response.json() 解析成Python 然后用data['commit']['author']['login']
  • 使用json.loadjson.loads将字符串变成python字典。
  • 如果你必须使用正则表达式,试试/"login"\s*:\s*"(?P<login>.*)"/g
  • 感谢@DanielRoseman,您的解决方案有效且非常优雅!没想到

标签: python regex python-3.x


【解决方案1】:

永远不要使用正则表达式解析 JSON,使用 JSON 解析器。

输入文件:

{
   "commit" : {
      "commit" : {
         "tree" : {
            "verification" : {
               "payload" : null,
               "verified" : false,
               "signature" : null,
               "reason" : "unsigned"
            },
            "sha" : "3e4710d0e021a0a7",
            "comment_count" : 0
         },
         "author" : {
            "id" : 5,
            "login" : "kyle"
         },
         "committer" : {
            "name" : "GitHub Enterprise",
            "date" : "2018-02-26T20:14:41Z"
         },
         "message" : "Update README.md"
      },
      "sha" : "adc3208a9ac76262250a"
   },
   "name" : "master"
}

命令:

$ jq '.commit.commit.author.login' file.json

或通过 脚本:

#!/usr/bin/env python3
import json

string = """
{ 
   "commit" : {
      "commit" : {
         "tree" : {
            "verification" : {
               "payload" : null,
               "verified" : false,
               "signature" : null,
               "reason" : "unsigned"
            },
            "sha" : "3e4710d0e021a0a7",
            "comment_count" : 0
         },
         "author" : {
            "id" : 5,
            "login" : "kyle"
         },
         "committer" : {
            "name" : "GitHub Enterprise",
            "date" : "2018-02-26T20:14:41Z"
         },
         "message" : "Update README.md"
      },
      "sha" : "adc3208a9ac76262250a"
   },
   "name" : "master"
}
"""

j = json.loads(string)
print(j['commit']['commit']['author']['login'])

输出:

"kyle"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-20
    • 2015-04-19
    • 1970-01-01
    • 2012-10-21
    • 2011-12-25
    • 1970-01-01
    相关资源
    最近更新 更多