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文件的更多相关文章

  1. python中操作csv文件

    python中操作csv文件 读取csv improt csv f = csv.reader(open("文件路径","r")) for i in f: pri ...

  2. Python读写操作Excel模块_xlrd_xlwt_xlutils

    Python 读写操作Excel -- 安装第三方库(xlrd.xlwt.xlutils.openpyxl) 如果仅仅是要以表单形式保存数据,可以借助 CSV 格式(一种以逗号分隔的表格数据格式)进行 ...

  3. python操作csv和excel文件

    1.操作csv文件 1).读取文件 import csv f=open("test.csv",'r') t_text=csv.reader(f) for t,i in t_text ...

  4. Python 读写操作Excel —— 安装第三方库(xlrd、xlwt、xlutils、openpyxl)

    数据处理是 Python 的一大应用场景,而 Excel 则是最流行的数据处理软件.因此用 Python 进行数据相关的工作时,难免要和 Excel 打交道. 如果仅仅是要以表单形式保存数据,可以借助 ...

  5. python读写操作文件

    with open(xxx,'r,coding='utf-8') as f:   #打开文件赋值给F ,并且执行完了之后不需要 f.close(). 在Python 2.7 及以后,with又支持同时 ...

  6. sqlserver如何读写操作windows系统的文件

    DECLARE   @object   int     DECLARE   @hr   int     DECLARE   @src   varchar(255),   @desc   varchar ...

  7. python读写hdf5及cdf格式文件

    Python write and read hdf5 file http://stackoverflow.com/questions/20928136/input-and-output-numpy-a ...

  8. python读写操作

    import sys 1 def test(): a=int(input()) x=[int(i) for i in input().split(' ')] y=[int(j) for j in sy ...

  9. 『无为则无心』Python基础 — 41、Python中文件的读写操作(一)

    目录 1.文件操作步骤 2.文件的读写操作 (1)文件的打开 (2)打开文件模式 (3)获取一个文件对象 (4)关于文件路径 1.文件操作步骤 当我们要读取或者写入文件时,我们需要打开文件,在操作完毕 ...

随机推荐

  1. Histogram

    folly/Histogram.h Classes Histogram Histogram.h defines a simple histogram class, templated on the t ...

  2. 强大的NCBI接口

    刚才小玩了下,不错,.net确实很方便,很强大 Using Entrez Utilities Web Service with C# and MS Visual Studio 2005 Updated ...

  3. Linux: su sudo sudoer

    日常操作中为了避免一些误操作,更加安全的管理系统,通常使用的用户身份都为普通用户,而非root.当需要执行一些管理员命令操作时,再切换成root用户身份去执行. 普通用户切换到root用户的方式有:s ...

  4. Tkinter Toplevel

       Tkinter Toplevel:顶层部件的工作,直接由窗口管理器管理的窗口.他们不必在它们上面的父widget   顶层部件的工作,直接由窗口管理器管理的窗口.他们不必在它们上面的父widge ...

  5. 「小程序JAVA实战」小程序我的个人信息-注销功能(42)

    转自:https://idig8.com/2018/09/06/xiaochengxujavashizhanxiaochengxuwodegerenxinxi-zhuxiaogongneng40/ 注 ...

  6. django -- url 的 默认值

    在urls.py里可以直接向函数传递默认值,看代码: urls.py from django.conf.urls import url from mytest import views urlpatt ...

  7. Oracle11gR2-聚簇因子浅析

    创建表t1,t2 SQL> conn n1/n1 Connected. SQL> SQL> SQL> create table t1 as select trunc(rownu ...

  8. Eclipse weblogic 中文乱码问题解决办法

  9. Quartz.NET文档 入门教程

    概述 Quartz.NET是一个开源的作业调度框架,非常适合在平时的工作中,定时轮询数据库同步,定时邮件通知,定时处理数据等. Quartz.NET允许开发人员根据时间间隔(或天)来调度作业.它实现了 ...

  10. MySQL 中随机获取数据

    由于需要大概研究了一下MYSQL的随机抽取实现方法. 目前采用的方法: SELECT * FROM tablename ORDER BY RAND() LIMIT 实现原理: 通过ORDER BY R ...