简体   繁体   English

Python - 正则表达式从 JSON 文本中获取动态用户名

[英]Python - regex to get dynamic usernames from JSON text

I am trying to extract value of 'login' from a dump of JSON which is in the form of text (response.text)我正在尝试从文本形式的 JSON 转储中提取“登录”的值 (response.text)

Here's the string:这是字符串:

{
   "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
         }

I am just trying to pull the value 'kyle' from the login in the last line.我只是想从最后一行的登录中提取值“kyle”。 The value of 'kyle' can change as it can be a different login each time. 'kyle' 的值可以更改,因为它每次都可以是不同的登录名。 Thus I need string in "login":"string"因此我需要“登录”中的字符串:“字符串”

Here's what I have right now but that only gets me "login" :这是我现在所拥有的,但这只会让我“登录”:

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

Never parse JSON with regex, use a JSON parser.永远不要用正则表达式解析 JSON,使用 JSON 解析器。

With :

Input file :输入文件 :

{
   "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"
}

Command :命令 :

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

Or via a script :或者通过脚本:

#!/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'])

Output :输出 :

"kyle"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM