def read_txt(path,i):
file = open(path,"rb")
text = []
str = ''
for line in file:
line = line.decode().lower()
str = str + line
text.append(str)
text.append(i)
return text
# 打开文件
path1 = "D:/document/data/review_polarity/txt_sentoken/neg"
path2 = "D:/document/data/review_polarity/txt_sentoken/pos"
dirs1 = os.listdir( path1 )
dirs2 = os.listdir( path2 )
texts = []
for p in dirs1:
texts.append(read_txt(path1+'/'+p,0))
for p in dirs2:
texts.append(read_txt(path2+'/'+p,1))
name = ['text','polarity']
test = pd.DataFrame(columns=name,data=texts)
test.to_csv('D:/document/data/review_polarity/txt_sentoken/All.csv')