Python将数据写入excel或者txt,读入csv格式或xls文件,写入csv(写一行空一行解决办法)
1.写入excel,一开始不需要自己新建一个excel,会自动生成
attribute_proba是我写入的对象

import xlwt
myexcel = xlwt.Workbook()
sheet = myexcel.add_sheet('sheet')
si=-1
sj=-1
for i in attribute_proba:
si=si+1
for j in i:
sj=sj+1
sheet.write(si,sj,str(j))
sj=-1
myexcel.save("attribute_proba_big.xls")
2.写入csv,注意要打上 newline="",否则会写一行空一行

import csv
with open("tech_problem.csv","w",newline="") as csvfile:
writer = csv.writer(csvfile)
for i in range(52):
writer.writerow([i,bol[i]])
slast的结构是有列表组成的列表
ls=[]
ls.append(number)
slast.append(ls)
可以直接写
import csv
with open("final.csv","w",newline="") as csvfile:
writer = csv.writer(csvfile)
writer.writerow(['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T'])
writer.writerows(slast)
3.写入txt,一开始就需要你新建一个txt文件

f=open('F:/goverment/myfinalcode/predict.txt', 'w')
for i in range(s):
f.write(str(predict[i]))
f.write('\n')
f.write("写好了")
f.close()
4.读入csv

file = 'F:/goverment/myfinalcode/test_big.csv'
fo=open(file)
ls=[]
for line in fo:
line=line.replace("\t",",")
line=line.replace("\n",",")
line=line.replace("\"",",")
ls.append(line.split(","))
for i in ls:
li=[]
for j in i:
if j == '':
continue
li.append(str(j))
testdata.append(li)

from pandas import read_csv
data_set = read_csv("F:/goverment/excel operating/type_in.csv")
data = data_set.values[:, :]
test_data = []
for line in data:
ls = []
for j in line:
ls.append(j)
test_data.append(ls)
5.读入xls

import xlrd
file = 'F:/goverment/myfinalcode/test_big_label.xls'
wb = xlrd.open_workbook(file)
ws = wb.sheet_by_name("Sheet1")
for r in range(ws.nrows):
col = []
for c in range(ws.ncols):
col.append(ws.cell(r, c).value)
testlabel.append(col)
Python将数据写入excel或者txt,读入csv格式或xls文件,写入csv(写一行空一行解决办法)的更多相关文章
- excel批量转换为CSV格式,xls批量导出csv格式
工具/原料 excel 2013 地址链接:http://pan.baidu.com/s/1c1ZABlu 密码:d3rc 方法/步骤 首选我们把需要导出为CVS的Excel文件整理集中到 ...
- 【matlab】将matlab中数据输出保存为txt或dat格式
将matlab中数据输出保存为txt或dat格式 总结网上各大论坛,主要有三种方法. 第一种方法:save(最简单基本的) 具体的命令是:用save *.txt -ascii x x为变量 *.txt ...
- 标注工具doccano导出数据为空的解决办法
地址:https://github.com/taishan1994/doccano_export doccano_export 使用doccano标注工具同时导出实体和关系数据为空的解决办法.docc ...
- python 导出数据到excel 中,一个好用的导出数据到excel模块,XlsxWriter
最近公司有项目需要导出数据到excel,首先想到了,tablib,xlwt,xlrd,xlwings,win32com[还可以操作word],openpyxl,等模块但是 实际操作中tablib 写入 ...
- Python导出数据到Excel表格-NotImplementedError: formatting_info=True not yet implemented
在使用Python写入数据到Excel表格中时出现报错信息记录:“NotImplementedError: formatting_info=True not yet implemented” 报错分析 ...
- 导出数据库数据制成Excel和txt
引用ICSharpCode.SharpZipLib.dll 1.编写压缩和解压代码 using System; using System.Collections.Generic; using Syst ...
- KETTLE单表同步,写入EXCEL和TXT
以下操作都在5.0.1版本下进行开发,其余版本可以进行自动比对 在平时工作当中,会遇到这种情况,而且很常见.比如:1.自动生成文件TXT或者EXCEL(电信行业该需求居多),上传至某服务器:2.双方数 ...
- 文件操作ofstream,open,close,ifstream,fin,依照行来读取数据, fstream,iosin iosout,fio.seekg(),文件写入和文件读写,文件拷贝和文件
1.ofstream,open,close 写入文件 #include<iostream> #include<fstream> using namespace std; ...
- php导出数据到excel,防止身份证等数字字符格式变成科学计数的方法
而关于php的也有,但是大多都是用phpExcel导出的方法或者spreadsheet等类或者控件之类的导出方法,而我所在维护的系统却用很简单的方法,如下,网上很少有讲如何设置要导出数据的EXcel格 ...
随机推荐
- Flask 1 Introductory Chapter
reference: <Flask Web 开发> Environment Python 3 Mac OSX Introductory Chapter: 安装 1.安装第三方工具 virt ...
- 阅读和设计源码利器UML
https://www.w3cschool.cn/uml_tutorial/ 就不做特殊说明了啊: 一 UML 1 简介 类注释: 下面的图表示的 UML 类,该图被分为四个部分. 顶端部分被用来命名 ...
- springboot模糊查询
在学习MyBatis过程中想实现模糊查询,可惜失败了.后来上百度上查了一下,算是解决了.记录一下MyBatis实现模糊查询的几种方式. 数据库表名为test_student,初始化了几条记录,如图: ...
- BZOJ1555 KD之死
如果没有必选的限制条件,就是水题了... 只要按照w + t排序就可以了,然后搞个堆来维护 于是有了限制条件,还是水题... 到了必选的时候强制选上,不加入堆中即可. /*************** ...
- PHP:第六章——正则表达式的基本概念
<?php header("Content-Type:text/html;charset=utf-8"); //正则表达式的基本概念: //宽松匹配和严格匹配: //常见的匹 ...
- POJ 1321 棋盘问题 dfs 难度:0
http://poj.org/problem?id=1321 注意是在'#'的地方放棋子 矩阵大小不过8*8,即使是8!的时间复杂度也足以承受,可以直接dfs求解 dfs时标注当前点的行和列已被访问, ...
- CF 148D Bag of mice 概率dp 难度:0
D. Bag of mice time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- bzoj1073
题意: k短路 题解: A* 当然是抄了zzd的代码 然而需要特判 为什么把bool改成int爆空间!!! 代码: #include<bits/stdc++.h> using namesp ...
- 在JavaScript中进行文件处理,第五部分:Blobs
译注:原文是<JavaScript高级程序设计>的作者Nicholas Zakas写的,本翻译纯属为自己学习而做,仅供参考.原文链接:这里 到目前为止,这个系列的帖子集中在和这些文件交互- ...
- 一个简单的观察者模式的JS实现
这不是原创文章,主要是写给自己看的.原文比较详细容易让人,我提取最简单最好理解的部分,便于我以后用到.参考http://www.cnblogs.com/TomXu/archive/2012/03/02 ...