简体   繁体   中英

how to refer variables in JSON file?

I have following JSON data which I'm trying to use in python.

{"tag": "currency",
         "patterns": ["What is dollar rate in PKR", "Convert Dollar to PKR", "Convert USD to PKR", "What is Euro rate", "Convert Euro to PKR", "GBP rate in PKR", "British Pound Rate in PKR"],
         "currencies": ["Dollar", "USD", "US Dollar", "Euro", "Pound", "British Pound", "GBP"],
         "responses": [""],

        "context_filter": ""
        },

Also below is my python script:

  if i['tag'] == 'currency':
     if i['currencies'] == 'USD' :
        engine.say(data.USD_PKR)
     if i['currencies'] == 'pound':
        engine.say('I can\'t tell about Pound Yet')

What I want to do is making this script check two conditions:

1: It checks if the tag is matched ( ie Currency )

2: It checks the currency names from currency names.

My python chatbot reads the data from this json file. I want the chatbot to tell me currency rates to 'PKR' which is currency of my country. the 'patterns' are basically the questions which user will ask from the chatbot. I can write code to get currency rates of above mentioned currencies

But I can't figure out how to write a code so that it tells me rate of only that currency which I asked it in command line. For example:

Me: What is USD rate?

Bot: 105.4 PKR

Me: What is GBP rate?

Bot: 110.66 PKR

Me: What is Euro rate?

Bot: 125.3 PKR

I can get the conversion rates one by one, by scrapping a website and printing the results to command line. So that part is done. I need to modify the Questions data so whenever a currency is mentioned in question, python script finds the rate for that particular currency which is mentioned in the Question. Hope I have explained it correctly.

Any suggestions?

You could use regexp:

import re

question = input('What do you want to do?')
currency_to_convert =  re.findall('What is (\S*)', question, re.S)
print (*currency_to_convert)

Will print out the currency you want to know the rate from...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM