How to process words from a csv list

Question:

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
Asked By: Evan Gertis

||

Answers:

csv.reader function is returning a reader 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.

message = ""
for q in questions:
   message= q+"n"
Answered By: Chris

It sounds like you can have multiple questions on the same line. If that is correct, try this:

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([question for row in questions for question in row])
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?
Answered By: Nineteendo
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.