Home > front end >  Parse git config file using git python
Parse git config file using git python

Time:10-18

So if I have a git config file something like this.

[color "branch"]
    current = yellow bold
    local = green bold
    remote = cyan bold

I want to read the text between in quotes. I tried using

repo_config = Repo(projects_dir "/" repo ".git")
color=repo_config.config_reader().get_value("color")

I can read the fields inside it like current,local,remote but I want to read the quoted text corresponding to color, how do I go about it

CodePudding user response:

configs = """
[color "branch"]
    current = yellow bold
    local = green bold
    remote = cyan bold
"""
file_name = 'test-config.txt'

with open(file_name,'w') as file:
    file.write(configs)

method 01

with open(file_name,'r') as file:
    file_txt = file.read()
    
print(file_txt.split('"')[1])

method 02

import configparser
config = configparser.ConfigParser()
config.read(file_name)
print(config.sections()[0].split(' ')[-1].replace('"',''))

output

## method 01
branch
## method 02
branch
  • Related