Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python: Fins String on File and return rext

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

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

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading