Python 在Json函数中传递列表
我一直在努力寻找正确的解决方案来处理从解析的Json对象获得的字符串 我想创建一个要从字符串中排除的单词列表。我在下面将此列表命名为“忽略”。我没有得到一个错误,但它并没有真正做到我想要的 下面是我的代码:Python 在Json函数中传递列表,python,list,function,Python,List,Function,我一直在努力寻找正确的解决方案来处理从解析的Json对象获得的字符串 我想创建一个要从字符串中排除的单词列表。我在下面将此列表命名为“忽略”。我没有得到一个错误,但它并没有真正做到我想要的 下面是我的代码: def extract_info(msg): created_time = msg['created_time'].replace('T', ' ').replace('+0000', '') ignore = ["you","all","has","can","that",
def extract_info(msg):
created_time = msg['created_time'].replace('T', ' ').replace('+0000', '')
ignore = ["you","all","has","can","that", "the"]
message = msg.get('message', 'Key "message" is not present.').replace('\n', '').replace(',', '').encode('utf8')
for ignore in message:
if ignore in message:
message = message.replace(ignore, '')
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)
上述代码的问题在于,它不会向创建的.csv文件写入任何内容。这只是一个空白字段
我想从“忽略”列表中删除“消息”中带有空格的任何单词,但现在我只有空格:您将
忽略
定义为一个列表,然后尝试在for循环中重复使用它,这可能会把一切都搞砸。此外,您不希望迭代字符串尝试执行以下操作:
ignore = ["you","all","has","can","that", "the"]
message = msg.get('message', 'Key "message" is not present.').replace('\n', '').replace(',', '').encode('utf8')
for word in message.split():
if word in ignore:
message = message.replace(word, '')
它不打印任何东西?那里没有一条打印语句。它写入的.csv文件将消息字段作为空白使用
print
功能。打印每一件你必须检查其值的东西。我这样做了,它只是空白。如果我在消息中删除“for ignore:If ignore in message:message=message.replace(ignore)”,它会工作,但我的忽略列表不会通过“mesage”文本传递