-3

I have a flat file with data in it and I am trying to search through a JSON dataset to get back the data. Here is how I am searching through the JSON dataset with a known variable:

for i in get_network_json:
    if i["name"] == str(store):
        network_id=(i["id"])
        print network_id

What I want to do is instead of using a variable for "store" I want to pull that data in from a flat file and iterate through a file and for each line in the file print the data. I am just not sure what to use for that.

Brad Solomon
  • 38,521
  • 31
  • 149
  • 235
  • As the question stands, why don't you just get rid of `if i["name"] == str(store):`? I think this needs more clarification. – roganjosh Sep 07 '17 at 19:54
  • Right now I am using that statement to enable me to find what I am looking for in the JSON data set with a known variable. I want to replace that variable with whatever comes in from the file that I want to use. – Joseph Jenkins Sep 07 '17 at 20:19
  • Is the problem that you don't initially know the data that you want to use from the flat file to pull information from the JSON dataset? – chickity china chinese chicken Sep 07 '17 at 20:25
  • I know the data I have and I know the data I want, I just don't know how to write the syntax to use that data in the flat file to search through the JSON data. – Joseph Jenkins Sep 07 '17 at 20:27
  • Okay, sounds pretty straightforward, maybe something like [Python: search for strings listed in one file from another text file?](https://stackoverflow.com/questions/12370342/python-search-for-strings-listed-in-one-file-from-another-text-file) – chickity china chinese chicken Sep 07 '17 at 23:08

1 Answers1

0

Give regex a shot. It should allow you to store the patterns you're looking for in another file (presumably in a list). You can then use os to import the file, grab the list, and iterate through the expressions to use for searching in your target file.

Python 3.6 Regular Express HOWTO

  • regex would work the issue is that I already the data in a file that I need. I just need to figure out the syntax to use the data in the file to search through the JSON data. – Joseph Jenkins Sep 07 '17 at 20:20
  • Is the data just a series of strings or a list already? Either way you'll want to get the data into a list and iterate through a for loop so that each item in the list is searched through your data. In that case you'll want to use "in." That would like kind of like this: foo = "This is a string that needs to be searched." bar = "string" >> foo in bar >> True –  Sep 11 '17 at 21:38