Fetching data from csv file

Hello Everyone,

I’m working on a chatbot project. I’m trying to fetch data from a csv file.My csv file consists of number of people and locations. Suppose the user asks for 10 people then the bot will answer about the location for 10 people.

I have tried using this code.

class ActionHelloWorld(Action):

 def name(self) -> Text:
     return "action_hello_world"

 def run(self, dispatcher: CollectingDispatcher,
         tracker: Tracker,
         domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:

     dispatcher.utter_message(text="Welcome to JRB Interlocutor!")

     return []

This is not giving me the required result. Any help would be highly appreciated?

Thanks

Hi @Rocky

Welcome to Rasa Community!

import csv
    class ActionHelloWorld(Action):
        def name(self) -> Text:
            return "action_hello_world"

        def run(self, dispatcher: CollectingDispatcher,
                tracker: Tracker,
                domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
     #Get Input from user
      limit = tracker.latest_message['text'].
       
      with open('location for your csv //Test.csv', 'r') as file:
               reader = list(csv.reader(file))
        
        for row in reader[:limit]:
                print("In Row")
                dispatcher.utter_message(text=row[1])
            return [] 

Sample CSV

Annotation 2020-05-27 153638 image

Hope it helps

Hi MuraliChandran,

Hope you are doing well. Thank you so much for your response. I tried just like you directed. When I typed 10, its not giving me any response.

Thanks & Regards

Hi @Rocky,

Is your custom actions getting triggered when you send 10 as input?

yes

Can you typecast string to int

for row in reader[:int(limit)]:

and also possible post errors if you get any?

okay sure. Thank you so much.

Regards

Hello Sir,

Hope you are doing well. I have corrected it just as you said and its working fine. But still I’m not getting the desired response. Suppose man.csv is my csv file.This csv file and image file of the output responses are attached below. Whatever I send like 10,20,30 etc. as user input, its giving me the same response “UK”. And when I send 1,2,3 etc., I’m the getting the desired responses.You can check the image file of the output. So, I think its considering the serial number as the user input, might be I’m wrong.

Do I need to first store the row data and then use it. Could you please help.

Thanks & Regards, man.csv (47 Bytes)

Hi @Rocky,

Please Post your nlu.md, stories.md and domain.yml and actions.py. I will look into it.

My apologies for the late reply. Please find the files attached with this message.

Thanks & Regards, nlu.md (848 Bytes) actions.py (2.0 KB) stories.md (535 Bytes) domain.yml (667 Bytes)

Hi @Rocky

I looked into your actions.py. You have commented out the part where the _csv needs to run and fetch data from .csv file. I have modified the actions.py and it will work now.

actions.py (2.0 KB)

Hi Sir,

I’m so sorry. By mistake I sent you the wrong file. I have tried it with this actions.py file. Please check. The file is attached with this message. My apologies for the inconvenience. Please help.actions.py (1.3 KB)

Hi, Yes, the one I above sent will work, I understood that you have set entities and intents in your files. so I modified the actions.py according to it…

The one you have attached right now.

dispatcher.utter_message(text=row[1]) is not inside the for loop that’s why it did not print the user input values you expected.

You can use the above one or the fix this actions.py by adjusting the dispatcher.utter_message(text=row[1]) inside the for loop. and change the import to import _csv and _csv.reader. Sometimes csv imports cannot be recognized on the py libraries. If you have issues on csv.

Okay Thank you so much Sir.

One more thing I have sent you a connection request through LinkedIn My name is Mushahid Abbasi. Could you please add me into your connection.

Regards

Sure :slight_smile:

Hi Sir,

Hope you are doing well. Sorry for the late reply. I was also trying by myself. Actually, whatever I’m giving as user input its returning each and every value of the location as response. Could you please help. The response image file is attached with this message. Please check and reply if possible.

Thanks & Regards,

Hi @Rocky,

It looks like you have only 6 cell of data in your .csv and you are asking for 10 cells, It will print everything. Try adding more data into your csv and see.

okay, sure

Thanks