我有这个函数,它接受两个输入.txt文件,删除标点符号,并添加句子pos或neg。 我想将这些文件的内容转换为小写 然后将这两个文件合并为单个文件名union.txt 但我的代码不起作用
def extractor (feature_select):
posFeatures = []
negFeatures = []
with open('positive.txt', 'r') as posSentences:
for i in posSentences:
posWords = re.findall(r"[\w']+|[(,.;:*@#/?!$&)]", i.rstrip())
posWords = [feature_select(posWords), 'pos']
posFeatures.append(posWords)
with open('negative.txt', 'r') as negSentences:
for i in negSentences:
negWords = re.findall(r"[\w']+|[(,.;:*@#/?!$&)]", i.rstrip())
negWords = [feature_select(negWords), 'neg']
negFeatures.append(negWords)
return posFeature, negFeature
filenames = [posFeature, negFeature]
with open('union.txt', 'w') as outfile:
for fname in filenames:
with open(fname) as infile:
outfile.write(infile.read())
答案 0 :(得分:0)
实际上,您正在尝试使用两个文件内容中的名称打开文件。 fname保存从输入文件中读取的内容。
filenames = [posFeature, negFeature]
with open('union.txt', 'w') as outfile :
for i in filenames : #refers to posFeature or negFeature which is a list
for j in i: #this loop reads each sentence from the list i
outfile.write(j) #it writes the sentence into outfile
无需回读已经读取并附加在posFeature和negFeature中的内容。上面的代码将直接在列表文件名中写入内容,现在合并了两个文件。