想把文件冒號後面的數字,按照每行寫入另一個文件,在每行末尾換行,用以下程式碼,有換行語句,結果還是沒換行,怎麼辦?
#!/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')
#