Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to process words from a csv list

I am running into an issue based on the following program.

Code

# Download the helper library from https://www.twilio.com/docs/python/install
import os
from twilio.rest import Client
import logging
import csv
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')


# initialization
MY_PHONE_NUMBER        = os.environ["MY_PHONE_NUMBER"]
TWILIO_PHONE_NUMBER    = os.environ["TWILIO_PHONE_NUMBER"]
TWILIO_ACCOUNT_SID     = os.environ["TWILIO_ACCOUNT_SID"]
TWILIO_AUTH_TOKEN      = os.environ["TWILIO_AUTH_TOKEN"]

# Configure Twillio
# Set environment variables for your credentials
# Read more at http://twil.io/secure
client = Client(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)
logging.debug(f"Connected to Twilio using MY_PHONE_NUMBER:{MY_PHONE_NUMBER},TWILIO_PHONE_NUMBER{TWILIO_PHONE_NUMBER}")

# Establish db connection

# Step 1: Set up frequency, i.e. times to send messages

# Step 2: Load questions
questionsFile   = open('questions.csv')
questions = csv.reader(questionsFile)
logging.debug(f"message:{questions}")

message = ""
for q in questions:
    message= q+"\n"

logging.debug(f"message:{message}")
# Step 3: Send questions
# message = client.messages.create(
#   body="Hello from Twilio",
#   from_=TWILIO_PHONE_NUMBER,
#   to=MY_PHONE_NUMBER
# )

# Step 4: Collect response

# Step 5: Save responses in db

# Next steps:
# 1. Process positive and negative sentiment from responses
# 2. Calculuate total positive sentiment
# 3. Calculate  total negative sentiment
# 4. Plot positive sentiment vs. negative sentiment
# https://stackoverflow.com/questions/74752681/what-is-the-best-way-to-perform-sentiment-analysis-by-using-text-message-respons

Expected

the words are processed and converted into a string

Actual

2022-12-13 14:32:42,165 - DEBUG - message:<_csv.reader object at 0x106ed05f0>
Traceback (most recent call last):
  File "/Users/evangertis/development/PythonAutomation/IGTS/TwilioMessaging/accountability.py", line 33, in <module>
    message= q+"\n"
TypeError: can only concatenate list (not "str") to list

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Does this help? Am I right that you can have multiple questions on the same line?

import os
import logging
import csv

# setting up defaults for testing
os.environ["MY_PHONE_NUMBER"] = "+1 (123) (456-7890"
os.environ["TWILIO_PHONE_NUMBER"] = "+1 (123) 456-7890"
os.environ["TWILIO_ACCOUNT_SID"] = "1234567890"
os.environ["TWILIO_AUTH_TOKEN"] = "0123456789abcdef"
with open("questions.csv", "w") as file:
    file.write("1 + 1 = ?,2 - 1 = ?\n")
    file.write("Who is the president of the United States?,What's the longest river in the world?")

logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
MY_PHONE_NUMBER        = os.environ["MY_PHONE_NUMBER"]
TWILIO_PHONE_NUMBER    = os.environ["TWILIO_PHONE_NUMBER"]
TWILIO_ACCOUNT_SID     = os.environ["TWILIO_ACCOUNT_SID"]
TWILIO_AUTH_TOKEN      = os.environ["TWILIO_AUTH_TOKEN"]
logging.debug(f"Connected to Twilio using MY_PHONE_NUMBER:{MY_PHONE_NUMBER},TWILIO_PHONE_NUMBER{TWILIO_PHONE_NUMBER}")
questionsFile   = open('questions.csv')
questions = csv.reader(questionsFile)
logging.debug(f"message:{questions}")
message = "\n".join()
logging.debug(f"message:{message}")

Output:

2022-12-13 21:23:45,274 - DEBUG - Connected to Twilio using MY_PHONE_NUMBER:+1 (123) (456-7890,TWILIO_PHONE_NUMBER+1 (123) 456-7890
2022-12-13 21:23:45,275 - DEBUG - message:<_csv.reader object at 0x7c12154c10>
2022-12-13 21:23:45,275 - DEBUG - message:1 + 1 = ?
2 - 1 = ?
Who is the president of the United States?
What's the longest river in the world?
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading