python2新建txt文件,并逐行写入数据
#coding=utf-8
import os
txtName = "codingWord.txt"
f=file(txtName, "a+")
for i in range(1,100):if i % 2 == 0:new_context = "C++" + '\n'f.write(new_context)else:new_context = "Python" + '\n'f.write(new_context)
f.close()
实际应用,合并libsvm所需要格式的两个txt特征值方法1:
python3新建txt文件,并逐行写入数据
#coding=utf-8
import os txtName = "imagenames.txt" f=open(txtName, "a+") f.write(new_context)
f.close()
方法2:
#coding=utf-8 import os
first=[] second=[] f=open('mergeTXT.txt','w') with open('first.txt', 'r') as f1:for line in f1:line=line.strip()first.append(line) with open('second.txt', 'r') as f2:for line2 in f2:line2=line2.strip()second.append(line2) for i in range(0,399):result=first[i]+'\t'+second[i]+'\n'f.write(result)
posted @ 2019-03-02 18:33 luyanjie 阅读(...) 评论(...) 编辑 收藏