Rumah > Soal Jawab > teks badan
Saya ingin menulis nombor selepas titik bertindih fail ke fail lain dalam setiap baris, dan balut baris di hujung setiap baris Gunakan kod berikut, terdapat pernyataan pemisah baris, tetapi hasilnya masih tiada baris rehat, apa yang perlu saya lakukan?
#!/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')
扔个三星炸死你2017-06-28 09:28:06
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')