Home > front end >  Python: Fins String on File and return rext
Python: Fins String on File and return rext

Time:07-09

I´m trying to find a specific string on a file and have it return the text in front of if.

The file has the following: "releaseDate": "2022-07-11T07:15:00.000Z"

I want to search the releaseDate but have it return the 2022-07-11T07:15:00.000Z

I can find it, but have honestly no idea how to return the info I need.

dateOccurence=open('scriptFile.txt', 'r').read().find('releaseDate')

CodePudding user response:

Your file is JSON content, so parsing it as this would be the best idea, but then you'd need to know the path of keys to reach it

A regex approach is easier here

with open('scriptFile.txt', 'r') as f:
    content = f.read()

date = re.search(r'"releaseDate": "([^"] )"', content)[1]
print(date)  # 2022-07-11T07:15:00.000Z
  • Related