[英]How to output the twilio sms logs in a csv file using Python API
I am using this script for downloading the sms log file from the Twilio. 我正在使用此脚本从Twilio下载短信日志文件。
https://github.com/asplunker/twilio-app/blob/master/bin/get_sms_logs.py https://github.com/asplunker/twilio-app/blob/master/bin/get_sms_logs.py
In the first run its downloading the file properly but in the second run it throws " index out of range error "
在第一次运行中,它正确下载了文件,但是在第二次运行中,它抛出了“
index out of range error "
So the error is suspected in the function : 因此,该函数中怀疑有错误:
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')
I am not sure if the output csv file in the first run doesn't specify each record in a single line. 我不确定第一次运行时输出的csv文件是否未在一行中指定每个记录。
Any pointers would be greatly appreciated. 任何指针将不胜感激。
I would add some error handling to find the state of objects at the time of failure. 我会添加一些错误处理,以查找故障时对象的状态。
Have you ever used try/except? 您曾经使用try / except吗? https://docs.python.org/2/tutorial/errors.html
https://docs.python.org/2/tutorial/errors.html
Basically you could set it up like this 基本上你可以这样设置
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
First thing I can think of is that your CSV file is not properly read. 我能想到的第一件事是未正确读取您的CSV文件。
The out of index error is probably happening here: 索引不足错误可能在这里发生:
date = line.split(',')[1]
Try adding a conditional for the date there: 尝试添加一个有条件的日期:
date_ = line.split(',') date = date_[1] if len(date_) >= 1 else ""
But you REALLY REALLY should have a look at CSV from standard library 但您真的应该看看标准库中的CSV
https://docs.python.org/2/library/csv.html https://docs.python.org/2/library/csv.html
The FAQ on how to export your SMS/Call logs shows a CSV example in PHP. 关于如何导出短信/呼叫日志的常见问题解答显示了PHP中的CSV示例。 I'm a Python person myself, but I would use this to test it out quick and dirty depending on the situation.
我本人是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.