逐行短信 - python & twilio

Line by line text messaging - python & twilio

我正在尝试从 .txt 文件中读取每一行,并使用 Twilio 将枚举数字逐行发送到 phone 数字(使用我自己的数字进行测试)。

下面正确读取文件,但只发送枚举列表的值。

所以,我收到:

Text Message 1: 1

Text Message 2: 2

Text Message 3: 3

而不是:

Text Message 1: 1: Hello!

Text Message 2: 2: This is working!

Text Message 3: 3: Last Line

f = open("file_name")
f = list(enumerate(f, start = 1))
    for line in f:
        text = line
        print text
        client = rest.TwilioRestClient(account_sid, auth_token)
        message = client.messages.create(body=text,
            to="Recipient_Number" 
            from_="Twilio_number")
        message.sid

这里是 Twilio 开发人员布道者。

当您使用 enumerate 时,它正在创建一个元组迭代器。在您的 for 循环中,您只检索每个元组中的第一项并发送它。您可以使用参数解构来获取索引和文本值,如下所示:

f = open("file_name")
f = enumerate(f, start = 1)
for index, line in f:
    text = index + ": " + line
    print text
    client = rest.TwilioRestClient(account_sid, auth_token)
    message = client.messages.create(body=text,
        to="Recipient_Number" 
        from_="Twilio_number")
    message.sid

值得注意的是,您也不需要使用 list 作为 enumerate returns 可以与 for ... in.

一起使用的迭代器

如果这有帮助,请告诉我。