我想把hello,'hi','gm'替换为'greatings',并把它们写在文本fi中

2024-09-24 12:33:40 发布

您现在位置:Python中文网/ 问答频道 /正文

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,higm替换为greetings,而不考虑大小写(比如hi,hola),并将其写入文本文件


Tags: inforifrecordelsewordanswersgood
1条回答
网友
1楼 · 发布于 2024-09-24 12:33:40

根据您的描述,您正在搜索列表中的某些项目,并尝试替换每个项目中的某些单词。但是,您可以将整个项替换为单词。如果这是一个问题,那么我建议使用String.replace函数,因为这只会替换单词,不会影响任何不包含单词的字符串。你知道吗

相关问题 更多 >