想把文件冒号后面的数字,按照每行写入另一个文件,在每行末尾换行,用以下代码,有换行语句,结果还是没换行,怎么办?
#!/usr/bin/python
#coding:utf-8
import pickle
import re
tfidf_dict={}
tfidf_all=[]
with open('/home/user1/zhouchun/lda/KNN/single_tfidf.txt','a') as file:
with open('/home/user1/zhouchun/lda/KNN/train_tfidf.txt', 'r') as fw:
# content = fw.readlines()
for line in fw:
index_tfidf=line.split()
# print index_tfidf
for j in index_tfidf:
m=re.compile(r'^(\d+):(\d+)$')
try:
word_index=m.match(j).group(1)
tfidf=m.match(j).group(2)
file.write(str(tfidf) + ' ')
except:
word_index=None
tfidf=None
file.write('\n')
windows上换行应该为
rn
. 所以file.write('rn')
.缩进不对。
雷雷