Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我想替换hello和#x27;嗨'';通用汽车公司';as';问候';并将其写入文本文件_Python - Fatal编程技术网

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]