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.文件操作步骤 当我们要读取或者写入文件时,我们需要打开文件,在操作完毕 ...
随机推荐
- IntelliJ Idea使用scalatest
背景:作为测试,开发写什么,测试自然就要测什么了,so = = 无scala基础,人较笨,折腾了两天才把环境弄好,如下: 一 IntelliJ Idea下载安装 这个真心是最简单的了 https:// ...
- JavaScript Post提交数据并跳转到页面(模拟Form表单提交)
function GotoWatchTicketCode() { var orderID='@ViewBag.OrderInfo.OrderID'; var phoneNum='@ViewBag.Or ...
- vue 起步_code
<template> <div class="hello"> <h1>{{ msg }}</h1> <div>{{dat ...
- 不可能的工作:在FBX模型导入脚本中生成模型的预置体
#if UNITY_EDITOR using System.Collections; using System.Collections.Generic; using System.IO; using ...
- iOS开发基础控件--UIButton
01 //这里创建一个圆角矩形的按钮 02 UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 03 ...
- SVN的“Invalid authz configuration”错误的解决方法
公司有人离职后,我把他svn账号删除 然后就报这个错了,我检查了authz文件,完全看不出什么错误.... 网上的各种方法试一遍,无果. 蹲个厕所,继续查这个问题 看到一个答案: 给不存在的组配置权限 ...
- Tensorflow CIFAR10 (二分类)
数据的下载: (共有三个版本:python,matlab,binary version 适用于C语言) http://www.cs.toronto.edu/~kriz/cifar-10-python. ...
- 不同Hadoop模式下,Hive元数据文件存储位置
假如在hive的配置文件hive-site.xml中,属性hive.metastore.warehouse.dir被设置为/root/hive/warehouse. 如果Hadoop是本地模式,则仓库 ...
- spring4-3-AOP-基于配置文件
1.建立业务类和切面类 2.在配置文件中配置bean 引入命名空间:
- [SoapUI] 通过编程的方式设置当前的Environment
testRunner.testCase.testSuite.project.setActiveEnvironment("Live")