热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

python创建一个txt文件python新建txt文件,并逐行写入数据

#codingutf-8txtNamecodingWord.txtffile(txtName,a)foriinrange(1,100):ifi%20:new_co

#coding=utf-8

txtName = "codingWord.txt"

f=file(txtName, "a+")

for i in range(1,100):

if i % 2 == 0:

new_context = "C++" + ' '

f.write(new_context)

else:

new_context = "Python" + ' '

f.write(new_context)

f.close()

实际应用,合并libsvm所需要格式的两个txt特征值

方法1:

#coding=utf-8

import numpy as np

import os

cwd = os.getcwd()

txtFile1 = cwd + '/first.txt'

txtFile2 = cwd + '/second.txt'

mergeFile2 = cwd + '/mergeTXT.txt'

f = file(mergeFile2, 'a+')

for (index1, line1) in enumerate(open(txtFile1)):

# print index1, line1

for (index2, line2) in enumerate(open(txtFile2)):

if index1 == index2:

newline = line1 + line2 + ' '

f.write(newline)

f.close()

方法2:

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]+' '+second[i]+' '

f.write(result)


推荐阅读
author-avatar
85中互动_715
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有