I have a js file that I need to get data out of but I am not sure how to get the data out in the way I want. I would like it to be readable and subscriptable like a python dict(like dict[“key/value”]).
just opening the file returns an <class ‘_io.TextIOWrapper’> but that doesn’t seem to be what I want.
Then reading that returns a more usable <class ‘str’> that I could use regex on but that seems unnecessary, I feel like there is a better way to do this.
with open(r'C:file.js') as dataFile: #returns <class '_io.TextIOWrapper'> print(type(dataFile)) #returns <class 'str'> print(type(dataFile.read()))
I have tried looking into the modules (json) and (json5) but I am not sure if they get me what I need either.
for clarity I want to get the “county_info” data out of this(https://ce.naco.org/app/data/general.js) site(which I have saved as a js file)
Advertisement
Answer
The county_info is the first object in there. So, just search for the delimiters”
import json data = open('x.js').read() i = data.find('{' ) j = data.find('}', i) data = json.loads(data[i:j+1]) print(data)