使用python读取excel文件并且执行相关操作
程序员文章站
2022-07-02 09:07:57
# -*- coding = utf-8 -*-#@Time: 2020/12/01 22:42#@Author: sun_0128#@File: test.py#@Software: #@Descriptionimport xlrdimport os#获取目录下所有的文件def listdir(path, list_name): #传入存储的list for file in os.listdir(path): file_path = os.path.join(...
# -*- coding = utf-8 -*-
#@Time: 2020/12/01 22:42
#@Author: sun_0128
#@File: test.py
#@Software:
#@Description
import xlrd
import os
#获取目录下所有的文件
def listdir(path, list_name): #传入存储的list
for file in os.listdir(path):
file_path = os.path.join(path, file)
if os.path.isdir(file_path):
listdir(file_path, list_name)
else:
list_name.append(file_path)
#准备一个文件路径的列表
directory = []
listdir('data',directory)
#计数使用
num=1
#循环遍历文件路径,读取文件数据,进行相关操作
for file_name in directory:
print("转换第%s个文件:"%num,end="")
print(file_name)
data = xlrd.open_workbook(file_name)
#获取所有工作表
sheet_name = data.sheet_names()
for name in sheet_name:
print("工作表:"+name)
#获取第二个工作表
table = data.sheet_by_name(sheet_name[1])
#行数 列数
rowNum = table.nrows
colNum = table.ncols
print(str(table.name)+"工作表有"+str(rowNum)+"行"+str(colNum)+"列")
#取第i行j列数据
# print(table.cell(0, 0).value)
#取第一列数据
a = table.col_values(0)
print("*************开始追加写入**********")
with open("c.txt","a") as f:
for line in a:
f.write(line+"\n")
num+=1
本文地址:https://blog.csdn.net/sun_0128/article/details/110457012
下一篇: 雀舌和竹叶青的区别有哪些,如何保存雀舌
推荐阅读
-
Windows下Python使用Pandas模块操作Excel文件的教程
-
Python使用xlrd读取Excel格式文件的方法
-
使用Python的openpyxl包操作Excel文件
-
又开始的python-day10-20200821-文件操作相关内置函数-拷贝-读取-写入
-
使用python读取excel文件并且执行相关操作
-
使用Python读取和修改Excel文件(基于xlrd、xlwt和openpyxl模块)
-
Python使用xlrd读取Excel格式文件的方法
-
使用Python操作excel文件
-
Python使用xlrd读取Excel格式文件的方法
-
使用Python操作Excel之xlsx文件介绍