Finding a variable in the .txt and comparing the values within two variables?
Question: 
    ----bDdefsYG3wqContent-Disposition: form-data;name="QueryResult"Content-Type: application/JSON;charset=utf-8-Context: efb3d3ce-ef50-4e83-8c31-063c3f5208aa{
        "status_code": 0,
        "result_type": "DRAGON_NLU_ASR_CMD",
        "NMAS_PRFX_SESSION_ID": "f786f0be-d547-4fca-8d72-96429a30c9db",
        "NMAS_PRFX_TRANSACTION_ID": "1",
        "audio_transfer_info": {
            "packages": [{
                "time": "20151221085512579",
                "bytes": 1633
            },
            {
                "time": "20151221085512598",
                "bytes": 3969
            }],
            "nss_server": "10.56.11.186:4503",
            "end_time": "20151221085512596",
            "audio_id": 1,
            "start_time": "20151221085512303"
        },
        "cadence_regulatable_result": "completeRecognition",
        "appserver_results": {
            "status": "success",
            "final_response": 0,
            "payload": {
                "actions": [{
                    "speaker": "user",
                    "type": "conversation",
                    "nbest_text": {
                        "confidences": [478,
                        0,
                        0],
                        "words": [[{
                            "stime": 0,
                            "etime": 1710,
                            "word": "ConnectedDrive\\*no-space-before",
                            "confidence": "0.241"
                        }],
                        [{
                            "stime": 0,
                            "etime": 1020,
                            "word": "Connected\\*no-space-before",
                            "confidence": "0.0"
                        },
                        {
                            "stime": 1020,
                            "etime": 1710,
                            "word": "drive",
                            "confidence": "0.0"
                        }],
                        [{
                            "stime": 0,
                            "etime": 900,
                            "word": "Connect\\*no-space-before",
                            "confidence": "0.0"
                        },
                        {
                            "stime": 900,
                            "etime": 980,
                            "word": "to",
                            "confidence": "0.0"
                        },
                        {
                            "stime": 980,
                            "etime": 1710,
                            "word": "drive",
                            "confidence": "0.0"
                        }]],
                        "transcriptions"= ["ConnectedDrive",
                        "Connected drive",
                        "Connect to drive"]
                    }
                }]
            }
        },
        "final_response": 0,
        "prompt": "",
        "result_format": "appserver_post_results"
    }-Disposition: form-data;name="QueryResult"Content-Type: application/JSON;charset=utf-8-Context: efb3d3ce-ef50-4e83-8c31-063c3f5208aa{
        "status_code": 0,
        "result_type": "DRAGON_NLU_ASR_CMD",
        "NMAS_PRFX_SESSION_ID": "f786f0be-d547-4fca-8d72-96429a30c9db",
        "NMAS_PRFX_TRANSACTION_ID": "1",
        "audio_transfer_info": {
            "packages": [{
                "time": "20151221085512579",
                "bytes": 1633
            },
            {
                "time": "20151221085512598",
                "bytes": 3969
            }],
            "nss_server": "10.56.11.186:4503",
            "end_time": "20151221085512596",
            "audio_id": 1,
            "start_time": "20151221085512303"
        },
        "cadence_regulatable_result": "completeRecognition",
        "appserver_results": {
            "status": "success",
            "final_response": 1,
            "payload": {
                "diagnostic_info": {
                    "adk_dialog_manager_status": "undefined",
                    "nlu_version": "[NLU_PROJECT:NVCCP-eng-USA];[Datapack:Version: nlps-eng-USA-NVCCP-6.1.100.12-2-GMT20151130160932];[VL-Models:Version: vlmodels-NVCCP-eng-USA-6.1.100.12-2-GMT20151130160335]",
                    "nlps_host": "mt-dmz-nlps002..com:8636",
                    "nlps_ip": "10.56.10.51",
                    "application": "AUDI_2017",
                    "nlu_component_flow": "[Input:VoiceJSON] [FieldID|auto_main] [NLUlib|C-eckart-r$Rev$.f20151118.1250] [build|G-r72490M.f20151130.1055] [vlmodel|Version: vlmodels-NVCCP-eng-USA-6.1.100.12-2-GMT20151130160335] [Flow|+VlingoTokenized]",
                    "third_party_delay": "0",
                    "nmaid": "AUDI_SDS_2017_EXT_20151203",
                    "nlps_profile": "AUDI_2017",
                    "fieldId": "auto_main",
                    "nlps_profile_package_version": "r159218",
                    "nlu_annotator": "com.NVCCP.eng-GBR.ncs51.VlingoNLU-client-qNVCCP_NCS51",
                    "ext_map_time": "2",
                    "nlu_use_literal_annotator": "0",
                    "int_map_time": "2",
                    "nlps_nlu_type": "nlu_project",
                    "nlu_language": "eng-GBR",
                    "timing": {
                        "finalRespSentDelay": "188",
                        "intermediateRespSentDelay": "648"
                    },
                    "nlps_profile_package": "AUDI_2017"
                },
                "actions": [{
                    "Input": {
                        "Interpretations": ["ConnectedDrive"],
                        "Type": "asr"
                    },
                    "Instances": [{
                        "nlu_classification": {
                            "Domain": "UDE",
                            "Intention": "Unspecified"
                        },
                        "nlu_interpretation_index": 1,
                        "nlu_slot_details": {
                            "Name": {
                                "literal": "ConnectedDrive"
                            },
                            "Search-phrase": {
                                "literal": "connecteddrive"
                            }
                        },
                        "interpretation_confidence": 4549
                    }],
                    "type": "nlu_results",
                    "api_version": "1.0"
                }],
                "nlps_version": "nlps(z):6.1.100.12.2-B359;Version: nlps-base-Zeppelin-6.1.100-B124-GMT20151130193521;"
            }
        },
        "final_response": 1,
        "prompt": "",
        "result_format": "appserver_post_results"
    }----_NMSP_vutc5w1XobDdefsYG3wq--
    

* * *

CODE:

    
    
    #!/usr/bin/env python
    import os
    import sys
    import time
    import webbrowser
    from io import BytesIO
    import uuid
    import httplib
    import StringIO
    import re
    import difflib
    import mmap
    import json
    
    directory =os.path.join("C:\Users\Desktop\Working\pcm-audio\English")
    for subdir, dirs, files in os.walk(directory): 
        for file in files:
            if file.endswith(".txt"): 
                content=json.load(file)
                if "status_code" in content:
                  if content["status_code"]==0:
                    print("valid")
    

I am reading a `.txt` file from a specific path as above. Later I am checking
for only `status_code` in the `.txt` file.

If it is 0 then I am printing as valid else Invalid. Later I am looking for
transcriptions and Interpretations variable in the `.txt` file (I showed in
the beginning, what it contains), which has a list of values.

Comparing those values, I compared it but I am not getting output. I am making
a mistake with RegEx here: `if line.find("transcriptions") ==
("Interpretations"):`

`status_code` is working fine. But not the `transcriptions` and
`Interpretations`.

Can someone tell me what is the mistake here?


Answer: Without details on the implementation of find this is diffictult to answer.

I would propose a completely different approach. The input you are reading
looks like valid JSON. Why not just load this with pythons json module. This
will give you a python dict with a very nice interface.

    
    
    import json
    info=json.load("filename.txt")
    if "status" in info:
      print(info["status"])
    

JSON has full support for arrays. You can add validation quite easily

    
    
    if "actions" in info:
      value=info["actions"]
      if not isinstance(value, list):
        raise TypeException("expecting a list of actions")
    

I would avoid using regular expressions. Right now the regex you are using is
easy to read but if your input parsing becomes more sophisticated, this will
surely change. Regexes are good for validation but I wouldn't try to parse
JSON with them.

Mind the difference between `json.loads` and `json.load`. Loads will try to
parse a string, load will read a file.

**EDIT:** I've read up on find. It seems to return a boolean. You shouldn't
compare that to a string. In python 3 this will always be false. This could be
a problem in your code.

**EDIT2:** Reading a list of files:

    
    
    filenames=["a.txt","b.txt","c.json","d.asdadf"] 
    for filename in filenames:   
      content = json.load(filename)   
      if "status" in content:
        print(filename+" contains a status entry")
        status_entry=content["status"]
        print("it is: "+str(status_entry))   
      else:
        print(filename+" does not contain a status entry")
    

With your setup, this should probably look like this, but I haven't tested
that yet:

    
    
    directory =os.path.join("C:\Users\hemanth_venkatappa\Desktop\Working\pcm-audio\English")
    for subdir, dirs, files in os.walk(directory): 
        for file in files:
            if file.endswith(".txt"): 
                content=json.load(file)
                if "status_code" in content:
                  if content["status_code"]==0:
                    print("valid")
    

You can use glob to get the filenames:
<https://docs.python.org/2/library/glob.html>

    
    
    import glob
    glob.glob("path_to_folder/*.txt")
    

will return a list of filenames, like this

    
    
    ["path_to_folder/a.txt", "path_to_folder/asdief.txt"]
    

You can use this list in the code above. Just be aware of supplying the
correct path to the folder. Python has a current working directory. The path
needs to be relative to this directory.

Take a look at os.getcwd() to get the current working directory and os.chdir()
to change it.

**Edit:**

The file is the result of some web query. It says

> Content-type : application/json

But of course the json parser will not be able to read that file. You didn't
include the output of your program, but I assume that json.load fails. The
error method should be something like "The file doesn't contain valid json" .
That's because you have the headers of the (http?)-response included in the
file. They need to be removed. You could do that manually, you could use
python, maybe with a regex. But the best way to do that would be not to store
the headers in the first place. Where do you generate the file ? I think you
should tweak that code first.

Honestly, I don't think you're really working on this. You just pasted my code
and didn't reflect on what you're doing. Read up on JSON, use a debugger and
look at each line of your code, think about the error message.

