I want to write the numbers after the colon of the file to another file in each line, and wrap the line at the end of each line. Use the following code, there is a line break statement, but the result is still no line break, what should I do?
#!/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')
The line break on windows should be
rn
. Sofile.write('rn')
.The indentation is wrong.