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; ...
随机推荐
- STL整理之map
转载请注明出处,部分内容引自李煜东<算法竞赛进阶指南> 前置知识: C++.C语言入门 Map是什么 Map是从键(key)到值(value)的映射,其内部实现是一棵以key为关键码 ...
- hdu2586 lca倍增法
倍增法加了边的权值,bfs的时候顺便把每个点深度求出来即可 #include<iostream> #include<cstring> #include<cstdio> ...
- LINQ学习之旅 C#3.0新特性(一)
一:C#3.0新语言的特性 自动属性(Auto-Implemented Properties) 隐含类型局部变量(Local Variable Type Inference) 匿名类型(Anonymo ...
- 伪类target实现纯css模态框
今天看到一个纯css模态框,觉得是很牛呀 看了下用了target伪类, 一直不知道有这么神奇的伪类 .. 用法是这样的,给模态框一个id, <div id="pop" cla ...
- day8--socket文件传输
FTP server 1.读取文件名 2.检测文件是否存在 3.打开文件 4.检测文件大小(告诉客户端发送文件的大小) 5.发送文件大小和MD5值给客户端,MD5 6.等待客户端确认(防止粘包) 7. ...
- 【AtCoder】KEYENCE Programming Contest 2019
A - Beginning 这个年份恐怕需要+2 #include <bits/stdc++.h> #define fi first #define se second #define p ...
- 用jQuery监听浏览器窗口的变化
$(window).resize(function () { //当浏览器大小变化时 alert($(window).height()); //浏览器时下窗口可视区域高度 alert($(docume ...
- poj2230 Watchcow【欧拉回路】【输出路径】(遍历所有边的两个方向)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4392 题目大意: 一个图,要将每条边恰好遍历两遍,而且要以不同的方向,还要回到原点. dfs解法 ...
- P2031 脑力达人之分割字串
P2031 脑力达人之分割字串字符串dp,f[i]表示主串到第i个字符,最多能分割成多少子串.f[i]=max(f[i],f[k]+1);k是能匹配到的前一位. #include<iostrea ...
- Docker启动mysql的坑2
正确启动mysql: docker run -p 3306:3306 --name mysql02 -e MYSQL_ROOT_PASSWORD=123456 -d mysql 此时虽然启动成功.但是 ...