python新建txt文件,并逐行写入数据
#coding=utf-8 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:
#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 + '\n'
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]+'\t'+second[i]+'\n'
f.write(result)
python新建txt文件,并逐行写入数据的更多相关文章
- python新建一个表格xls并写入数据
# -*- coding:utf-8 -*- import xlwt workbook = xlwt.Workbook() # 新建一个工作簿 sheet = workbook.add_sheet(& ...
- python操作txt文件中数据教程[1]-使用python读写txt文件
python操作txt文件中数据教程[1]-使用python读写txt文件 觉得有用的话,欢迎一起讨论相互学习~Follow Me 原始txt文件 程序实现后结果 程序实现 filename = '. ...
- python操作txt文件中数据教程[4]-python去掉txt文件行尾换行
python操作txt文件中数据教程[4]-python去掉txt文件行尾换行 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文章 python操作txt文件中数据教程[1]-使用pyt ...
- python操作txt文件中数据教程[3]-python读取文件夹中所有txt文件并将数据转为csv文件
python操作txt文件中数据教程[3]-python读取文件夹中所有txt文件并将数据转为csv文件 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 python操作txt文件中 ...
- python操作txt文件中数据教程[2]-python提取txt文件
python操作txt文件中数据教程[2]-python提取txt文件中的行列元素 觉得有用的话,欢迎一起讨论相互学习~Follow Me 原始txt文件 程序实现后结果-将txt中元素提取并保存在c ...
- web端自动化——Python读取txt文件、csv文件、xml文件
1.读取txt文件 txt文件是我们经常操作的文件类型,Python提供了以下几种读取txt文件的方式. 1)read(): 读取整个文件. 2)readline(): 读取一行数据. 3)readl ...
- Python读取txt文件
Python读取txt文件,有两种方式: (1)逐行读取 data=open("data.txt") line=data.readline() while line: print ...
- Python: 把txt文件转换成csv
最近在项目上需要批量把txt文件转成成csv文件格式,以前是手动打开excel文件,然后导入txt来生产csv文件,由于这已经变成每周需要做的事情,决定用python自动化脚本来实现,思路: 读取文件 ...
- 代码实现将键盘录入的数据拷贝到当前项目下的text.txt文件中,键盘录入数据当遇到quit时就退出
package com.looaderman.test; import java.io.FileNotFoundException; import java.io.FileOutputStream; ...
随机推荐
- poj2185 kmp求最小覆盖矩阵,好题!
/* 特征值k=m-next[m]就是最小循环节的长度, m%k就是去末尾遗留长度 */ #include<iostream> #include<cstring> #inclu ...
- python 全栈开发,Day123(图灵机器人,web录音实现自动化交互问答)
昨日内容回顾 . 百度ai开放平台 . AipSpeech技术,语言合成,语言识别 . Nlp技术,短文本相似度 . 实现一个简单的问答机器人 . 语言识别 ffmpeg (目前所有音乐,视频领域,这 ...
- ubuntu 12.04 安装 openssh-server 失败,请问怎么该弄?
$ sudo apt-get install openssh-server Reading package lists... Done Building dependency tree Reading ...
- 《剑指offer》-二叉搜索树与双向链表
输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不能创建任何新的结点,只能调整树中结点指针的指向. 题目的描述不是很习惯.题目的意思是把二叉树从左到右遍历,相当于双向链表的遍历. 其实 ...
- ERP商品管理业务逻辑封装(三十四)
产品购进管理业务逻辑: public class ProductBLL { /// <summary> /// 产品对象添加 并且返回产品编号 /// </summary> / ...
- Windows10 Compatibility Telemetry(CompatTelRunner.exe) 占用硬盘100%
相信很多人跟我一样总被Compatibility Telemetry(CompatTelRunner.exe) 占用硬盘100%困扰,Compatibility Telemetry翻译过来就是“微软兼 ...
- Django ORM中使用update_or_create功能再解
以前,我解过这个问题,现在百度搜索,发了像也只能找到我这个帖子. https://www.cnblogs.com/aguncn/p/4922654.html 今天,看了看官方文档,关于这个update ...
- Hibernate的核心对象关系映射
Hibernate的核心就是对象关系映射: 加载映射文件的两种方式: 第一种:<mapping resource="com/bie/lesson02/crud/po/employee. ...
- Minimum Transport Cost HDU1385(路径打印)
最短路的路径打印问题 同时路径要是最小字典序 字典序用floyd方便很多 学会了两种打印路径的方法!!! #include <stdio.h> #include <string.h& ...
- Python爬虫之Beautiful Soup解析库的使用(五)
Python爬虫之Beautiful Soup解析库的使用 Beautiful Soup-介绍 Python第三方库,用于从HTML或XML中提取数据官方:http://www.crummv.com/ ...