Search code examples
pythonpython-re

How to correctly search in Python3 and get the value?


I am trying to read a .env style file and pull out the content using regex. The line I want to search and get is:

APP_SECRET=test

I want to get the test. However, when I try search, I get None:

import re


if "APP_SECRET" in RESPONSE:
    print(re.search('APP_SECRET', RESPONSE)) #None

RESPONSE does have APP_SECRET=test in. Any help appreciated.

I assign response like so:

RESPONSE = REQ.get(f"{NBASE}/download?file={DATA['file']}&c={DATA['c']}").text

Solution

  • You can just read through each line of the response and split on = and check if the left side has "APP_SECRET", like

    for line in RESPONSE.split('\n'):
        params = line.split('=')
        if params[0] == 'APP_SECRET':
            val = params[1]
            break
    print(val)