亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

python - Why is there no line break in the result after writing a newline statement?
天蓬老師
天蓬老師 2017-06-28 09:26:06
0
3
1266

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')

天蓬老師
天蓬老師

歡迎選擇我的課程,讓我們一起見證您的進步~~

reply all(3)
我想大聲告訴你

The line break on windows should be rn. So file.write('rn').

洪濤

The indentation is wrong.

扔個三星炸死你
with open('a.txt', 'r') as f1, open('b.txt', 'a') as f2:
    for line in f1:
        new_line = [_.split(':')[1] for _ in line.split()]
        f2.write(' '.join(new_line))
        f2.write('\n')
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template