python读写操作csv及excle文件
1、python读写csv文件
import csv #读取csv文件内容方法1
csv_file = csv.reader(open('testdata.csv','r'))
next(csv_file, None) #skip the headers
for user in csv_file:
print(user) #读取csv文件内容方法2
with open('testdata.csv', 'r') as csv_file:
reader = csv.reader(csv_file)
next(csv_file, None)
for user in reader:
print(user) #从字典写入csv文件
dic = {'fengju':25, 'wuxia':26}
csv_file = open('testdata1.csv', 'w', newline='')
writer = csv.writer(csv_file)
for key in dic:
writer.writerow([key, dic[key]])
csv_file.close() #close CSV file csv_file1 = csv.reader(open('testdata1.csv','r'))
for user in csv_file1:
print(user)
2、python读写excle文件
需要先用python pip命令安装xlrd , xlwt库~
import xlrd, xlwt #xlwt只能写入xls文件 #读取xlsx文件内容
rows = [] #create an empty list to store rows
book = xlrd.open_workbook('testdata.xlsx') #open the Excel spreadsheet as workbook
sheet = book.sheet_by_index(0) #get the first sheet
for user in range(1, sheet.nrows): #iterate 1 to maxrows
rows.append(list(sheet.row_values(user, 0, sheet.ncols))) #iterate through the sheet and get data from rows in list
print(rows) #写入xls文件
rows1 = [['Name', 'Age'],['fengju', ''],['wuxia', '']]
book1 = xlwt.Workbook() #create new book1 excle
sheet1 = book1.add_sheet('user') #create new sheet
for i in range(0, 3):
for j in range(0, len(rows1[i])):
sheet1.write(i, j, rows1[i][j])
book1.save('testdata1.xls') #sava as testdata1.xls
python读写操作csv及excle文件的更多相关文章
- python中操作csv文件
python中操作csv文件 读取csv improt csv f = csv.reader(open("文件路径","r")) for i in f: pri ...
- Python读写操作Excel模块_xlrd_xlwt_xlutils
Python 读写操作Excel -- 安装第三方库(xlrd.xlwt.xlutils.openpyxl) 如果仅仅是要以表单形式保存数据,可以借助 CSV 格式(一种以逗号分隔的表格数据格式)进行 ...
- python操作csv和excel文件
1.操作csv文件 1).读取文件 import csv f=open("test.csv",'r') t_text=csv.reader(f) for t,i in t_text ...
- Python 读写操作Excel —— 安装第三方库(xlrd、xlwt、xlutils、openpyxl)
数据处理是 Python 的一大应用场景,而 Excel 则是最流行的数据处理软件.因此用 Python 进行数据相关的工作时,难免要和 Excel 打交道. 如果仅仅是要以表单形式保存数据,可以借助 ...
- python读写操作文件
with open(xxx,'r,coding='utf-8') as f: #打开文件赋值给F ,并且执行完了之后不需要 f.close(). 在Python 2.7 及以后,with又支持同时 ...
- sqlserver如何读写操作windows系统的文件
DECLARE @object int DECLARE @hr int DECLARE @src varchar(255), @desc varchar ...
- python读写hdf5及cdf格式文件
Python write and read hdf5 file http://stackoverflow.com/questions/20928136/input-and-output-numpy-a ...
- python读写操作
import sys 1 def test(): a=int(input()) x=[int(i) for i in input().split(' ')] y=[int(j) for j in sy ...
- 『无为则无心』Python基础 — 41、Python中文件的读写操作(一)
目录 1.文件操作步骤 2.文件的读写操作 (1)文件的打开 (2)打开文件模式 (3)获取一个文件对象 (4)关于文件路径 1.文件操作步骤 当我们要读取或者写入文件时,我们需要打开文件,在操作完毕 ...
随机推荐
- 安卓权限处理 PermissionDog
PermissionDog 简介 权限狗 权限申请 最近在一家公司实习,项目中需要用到适配安卓6.0以上的系统,我本来是想用其他人已经写好的权限申请框架来实现的,但是发现跟我的需求有点小区别,所以就自 ...
- quartz报错 Couldn't retrieve job because the BLOB couldn't be deserialized: null
今天线上添加定时任务之后 定时任务查询页面报出如上错误, 原因有两点 1.org.quartz.jobStore.useProperties = true 这个属性的意思存储的JobDataMaps是 ...
- [Z]CiteSeer统计的计算机领域的期刊和会议的影响因子
稍微有点早,04年的,但很全 http://emuch.net/html/200510/128286.html 另附一个不明来路的排名:http://www.wi2.uni-erlangen.de/_ ...
- C# 设计模式-单例模式(Singleton)
所谓单例模式即所谓的一个类只能有一个实例,说白了,也就是类只能在内部实例一次,然后提供这一实例,外部无法对此类实例化. 单例模式的特点: 1.只能有一个实例: 2.只能自己创建自己的唯一实例: 3.必 ...
- leetcode189
public class Solution { public void reverse(int[] nums, int start, int end) { while (start < end) ...
- e s6 知识点
http://es6.ruanyifeng.com/#docs/reflect 1.资料对es6箭头函数中的this总结:箭头函数中的this指向的是 定义时的this,而不是 执行时的this.
- MySQL篇之Navicat可视化工具
主要内容: Navicat工具的介绍和使用 1.介绍和下载安装 <1>介绍 Navicat是一款针对MySQL数据库开发的可视化管理工具,以图形界面的形式操作MySQL数据库. 但在生产环 ...
- java aop 日志打印 正则设置
package tz.lion.Utils.aop; import com.alibaba.fastjson.JSON;import org.springframework.web.multipart ...
- git 撤销 merging
当我们在合代码的时候经常会遇到一些问题,这时候分支就处于merging状态,这时候可以用下面的命令撤销 $ git reset --hard HEAD (or sha_1) 不知道有没有更好的办法,希 ...
- Texture Format全解析
[Texture Format全解析] What internal representation is used for the texture. This is a tradeoff between ...