繁体   English   中英

如何使用Python API在CSV文件中输出twilio短信日志

[英]How to output the twilio sms logs in a csv file using Python API

我正在使用此脚本从Twilio下载短信日志文件。

https://github.com/asplunker/twilio-app/blob/master/bin/get_sms_logs.py

在第一次运行中,它正确下载了文件,但是在第二次运行中,它抛出了“ index out of range error "

因此,该函数中怀疑有错误:

def write_records():
# avoid duplicates

data = []
if os.path.exists(LOG_FILE):
    with codecs.open(LOG_FILE) as d:
        file_data = d.readlines()
        for line in file_data:
            print line
            date = line.split(',')[1]
            if  date == LAST_ENTRY:
                    data.append(date)

with codecs.open(LOG_FILE, 'a') as f:
    for record in reversed(RECORDS):
        if not record.split(',')[1] in data:
            f.write(record)
            f.write('\n')

我不确定第一次运行时输出的csv文件是否未在一行中指定每个记录。

任何指针将不胜感激。

我会添加一些错误处理,以查找故障时对象的状态。

您曾经使用try / except吗? https://docs.python.org/2/tutorial/errors.html

基本上你可以这样设置

def write_records():
# avoid duplicates
    try:
        data = []
        if os.path.exists(LOG_FILE):
            with codecs.open(LOG_FILE) as d:
                file_data = d.readlines()
                for line in file_data:
                    print line
                    date = line.split(',')[1]
                    if  date == LAST_ENTRY:
                            data.append(date)

        with codecs.open(LOG_FILE, 'a') as f:
            for record in reversed(RECORDS):
                if not record.split(',')[1] in data:
                    f.write(record)
                    f.write('\n')

    except IndexError:
        #log variables here and examine the issue closely

我能想到的第一件事是未正确读取您的CSV文件。

索引不足错误可能在这里发生:

date = line.split(',')[1]

尝试添加一个有条件的日期:

date_ = line.split(',') date = date_[1] if len(date_) >= 1 else ""

但您真的应该看看标准库中的CSV

https://docs.python.org/2/library/csv.html

关于如何导出短信/呼叫日志的常见问题解答显示了PHP中的CSV示例。 我本人是Python使用者,但我会根据情况使用它来快速而又肮脏地对其进行测试。

<!--?php <br ?-->/**
 * Download the library from: https://github.com/twilio/twilio-php
 * Copy the 'Services' folder into a directory containing this file.
 */
require('Services/Twilio.php');

$account_sid = "ACXXXXXXXXX"; // Your Twilio account sid
$auth_token = "YYYYYYYYYYYY"; // Your Twilio auth token

// Download data from Twilio API
$client = new Services_Twilio($account_sid, $auth_token);
$messages = $client->account->sms_messages->getIterator(0, 50, array(
    'DateSent>' => '2012-09-01',
    'DateSent<' => '2012-09-30',
    //'From' => '+17075551234', // **Optional** filter by 'From'...
    //'To' => '+18085559876', // ...or by 'To'
));

// Browser magic
$filename = $account_sid."_sms.csv";
header("Content-Type: application/csv") ;
header("Content-Disposition: attachment; filename={$filename}");

// Write headers
$fields = array(
    'SMS Message SID', 'From', 'To', 'Date Sent',
    'Status', 'Direction', 'Price', 'Body'
);
echo '"'.implode('","', $fields).'"'."\n";

// Write rows
foreach ($messages as $sms) {
    $row = array(
        $sms->sid, $sms->from, $sms->to, $sms->date_sent,
        $sms->status, $sms->direction, $sms->price, $sms->body
    );
    echo '"'.implode('","', $row).'"'."\n";
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM