Python 我想替换hello和#x27;嗨'';通用汽车公司';as';问候';并将其写入文本文件
我试过这个Python 我想替换hello和#x27;嗨'';通用汽车公司';as';问候';并将其写入文本文件,python,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['questionTe
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()
但是我还需要将hello、
hi
、gm
替换为问候语
,而不考虑大小写(如hi,hola),并将其写入到文本文件中从您的描述中,您正在搜索列表中的一些项目,并尝试替换每个项目中的一些单词。但是,您可以将整个项目替换为单词。如果这是一个问题,那么我建议使用function,因为这只会替换单词,不会影响任何不包含单词的字符串。您到底需要什么?我不明白!
lis = ['greetings' if word in gre else word for word in lis0]