PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, July 25, 2022

[FIXED] How get data stored in vars from a js file in python

 July 25, 2022     javascript, json, python     No comments   

Issue

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)


Solution

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)


Answered By - Tim Roberts
Answer Checked By - Robin (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing