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
2
Answers
csv.reader
function is returning areader
object which les you iterate over lines in the CSV file. Each line is a list of strings.Consequently
q + "/n"
is asking Python to add a string to a list.Note also that the following resets
message
on each iteration and so ignores all but the last line in the CSV file.It sounds like you can have multiple questions on the same line. If that is correct, try this:
Output: