Home > Software engineering >  Skip Rows in CSV to List Python
Skip Rows in CSV to List Python

Time:06-24

I am new to python and am trying to build an sms app with twilio and am running into the following issue. I have a csv file that contains 3 columns: Numbers, Name, Link i converted each column to a separate list by doing:

from pandas import *

columns = read_csv("file path")
phone_number = columns['Numbers'].tolist()
last_name = columns['Name'].tolist()
link = columns['Link'].to_list()

Im trying to skip "rows" if a certain number is found. For example if Number = 8001234567 i want to skip that and its associated name an link and continue messaging the other numbers, names and links out. this is what i have so far:

def send_message(num, lname, link):
    message = twilio_client.messages.create(
    body= 'Hi '   lname    ',\n\n This is a test. Heres a link: '   link, 
    from_= my_number, 
    to= num)

for (n, ln, lk) in zip(phone_number, last_name, link):
    if n =='8001234567':
        continue
    time.sleep(5)
    send_message(num=n, lname=ln, link=lk)

No message is being sent for some reason but if i delete parameters and variables out of body it sends the right amount of messages.

CodePudding user response:

I have reformatted your code a bit. twilio_client.messages.create accepts body, from_, to as strings only.

import pandas as pd


df = pd.read_csv("file path")
df = df[df['Numbers'].astype(str) != '8001234567']

def send_message(num, lname, link):
    message = twilio_client.messages.create(
    body= f'Hi {lname} \n\n This is a test. Heres a link:', 
    from_= my_number
    media_url=[link], 
    to= num)

for i in range(df.shape[0])
    n, ln, lk = df.iloc[i,:].values
    send_message(num=str(n), lname=str(ln), link=str(lk))
    time.sleep(5)
  • Related