以下是我查询Facebook的代码。
我想将实际邮件的总大小限制为980 varchar。
def write_csv(fname, rows, header=None, append=False, **kwargs):
filemode = 'ab' if append else 'wb'
with open(fname, filemode) as outf:
out_csv = csv.writer(outf, **kwargs)
if header:
out_csv.writerow(header)
out_csv.writerows(rows)
def main():
ts = FacebookSearch()
data = ts.search('appliance')
js = json.loads(data)
messages = ([msg['created_time'], msg.get('message', 'Key "message" is not present.').replace('\n', '').encode('utf8'), msg['from']['id']] for msg in js.get('data', []))
#def messages(s, l): ## breaks here
#return s if len(s)<=l else s[0:l-3]+'...' ## Breaks here
write_csv('fb_washerdryer.csv', messages, append=True)
if __name__ == '__main__':
main()
我上面尝试在我的函数中定义它,但是我收到了这个错误:
Traceback (most recent call last):
File "./facebook_washer_dryer8.sh", line 52, in <module>
main()
File "./facebook_washer_dryer8.sh", line 48, in main
write_csv('fb_washerdryer.csv', messages, append=True)
File "./facebook_washer_dryer8.sh", line 35, in write_csv
out_csv.writerows(rows)
TypeError: writerows() argument must be iterable
我以为我做的是正确的事,但现在我有点困惑。
答案 0 :(得分:0)
我认为这就是你想要的:
...
#Create the shortening function
def short_message(s, l):
#return s if len(s)<=l else s[0:l-3]+'...'
#define a variable to be shortened if too long
mess = msg.get('message', 'Key "message" is not present.').replace('\n', '').encode('utf8')
#pass the shortened message
write_csv('fb_washerdryer.csv', short_message(mess, 980), append=True)