替换Json对象函数中的List

时间:2014-01-22 16:34:10

标签: python list function replace

我一直在玩

以下是我的代码:

def extract_info(msg):
    created_time = msg['created_time'].replace('T', ' ').replace('+0000', '')
    mess = msg.get('message', 'Key "message" is not present.').replace('\n', '').replace(',', '').encode('utf8') 
    message = (mess[:498] + '..') if len(mess) > 498 else mess
    user_id = msg['from']['id']
    return (created_time, message, user_id)



def main():
    ts = FacebookSearch()
    data = ts.search('dishwasher')
    js = json.loads(data)
    messages = (extract_info(msg) for msg in js.get('data', []))
    write_csv('fb_dishwasher.csv', messages, append=True)

上述代码存在问题:

我想通过替换使用忽略列表来操纵'消息'。

有点想做什么:

ignore = ["you","all","has","can","that", "the", "what", "with", "and", "to", "this", "would","from", "your", "which", "while", "these", "when", "way", "like", "been", "will", "look"]

def replaceall(s, olds, replacement):
    for old in olds:
        s = s.replace(old, replacement)
    return s

最大的问题是我不知道如何将上面的忽略列表混合到我上面的'extract_info'的现有函数中。我是否创建了一个新函数,或者在现有函数中创建嵌套函数。 (感觉不对劲)。我真的想对实现这一目标的最佳方法提出一些想法和想法。

1 个答案:

答案 0 :(得分:0)

似乎extract_info每次都会收到单个消息,也许您最好在那里使用e函数,而不是一次性对所有消息进行重新连接,即:

message = message.replace(old, replacement)