gre =['Hi','Hello','Hey','Whats up','Greetings','Hola','Good morning','Good afternoon','Good evening']
qus = []
quslist = db.questions.find({'date':{'$lt':dn, '$gt':dtr}})
for answers_record in quslist:
qus.append(answers_record['questionText'])
lis0 = [h.encode('utf-8') for h in qus]
lis = ['greetings' if word in gre else word for word in lis0]
print"day",lis
thefile = open('test.txt', 'w')
for item in lis:
thefile.write("%s\n" % item)
thefile.close()
我试过了
lis = ['greetings' if word in gre else word for word in lis0]
但是我还需要将hello,hi
,gm
替换为greetings
,而不考虑大小写(比如hi,hola),并将其写入文本文件
根据您的描述,您正在搜索列表中的某些项目,并尝试替换每个项目中的某些单词。但是,您可以将整个项替换为单词。如果这是一个问题,那么我建议使用String.replace函数,因为这只会替换单词,不会影响任何不包含单词的字符串。你知道吗
相关问题 更多 >
编程相关推荐