欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python处理excel与txt文件详解

程序员文章站 2022-03-09 23:04:39
目录一、python处理excel文件1. 两个头文件2. 读取excel文件3. 写入excel文件二、python处理txt文件1. 打开txt文件2. 读取txt文件3. 写入txt文件(需注意...

一、python处理excel文件

1. 两个头文件

import xlrd
import xlwt

其中xlrd模块实现对excel文件内容读取,xlwt模块实现对excel文件的写入。

2. 读取excel文件

Python处理excel与txt文件详解

# 打开excel文件
workbook = xlrd.open_workbook(excelpath)
# 获取所有的sheet的名字
allsheetnames = workbook.sheet_names()
print(allsheetnames)

输出:[‘sheet1', ‘sheet2']

# 按索引号获取sheet的名字(string类型)
sheet1name = workbook.sheet_names()[1]
print(sheet1name)

输出:sheet2

# 指定选择第二个sheet
sheet1_content1 = workbook.sheet_by_index(1)  

# 获取第二个sheet中的 某一列 数据,index为 列 的编号
content = sheet1_content1.col_values(index)
print(content )

输出:[‘50_female_cns', 0.0001450627129261498, 0.00014610459059353443, 0.0001005863347657359, 6.582112999369104e-05, 0.00012061284774544405, ' ', 0.00012075268247024065, 9.77776267815119e-05, 0.00012586155938565746, 0.0003279103274939261, 0.00022441965601437833 …]

# 指定选择第二个sheet
sheet1_content1 = workbook.sheet_by_index(1)  

# 获取第二个sheet中的 某一行 数据,index为 行 的编号
content = sheet1_content1.row_values(index)
print(content)

输出:['', 0.0001450627129261498, 0.00017014314076560212, 0.00018181811940739254, 0.0003775072437995825, 0.00042918333947459267, 0.0004889411346133797, 0.0001635510979069336, 0.00018714823789391146, 0.0002130216204564284, 0.0004294577819371397, 0.0004909460429236959, 0.0005394823288641913]

3. 写入excel文件

# 初始化写入环境
workbook = xlwt.workbook(encoding='utf-8')
# 创建一个 sheet
worksheet = workbook.add_sheet('sheet')
# 调用 write 函数将内容写入到excel中, 注意需按照 行  列  内容 的顺序
worksheet.write(0, 0, label='car type')
worksheet.write(0, 1, label='50_female_cns')
worksheet.write(0, 2, label='75_female_cns')
worksheet.write(0, 3, label='95_female_cns')

# 保存 excel
workbook.save("你的路径")

二、python处理txt文件

1. 打开txt文件

#方法1,这种方式使用后需要关闭文件
f = open("data.txt","r")
f.close()

#方法2,使用文件后自动关闭文件
with open('data.txt',"r") as f: 

打开文件的模式主要有,r、w、a、r+、w+、a+
r:以读方式打开文件,可读取文件信息。
w:以写方式打开文件,可向文件写入信息。如文件存在,则清空该文件,再写入新内容
a:以追加模式打开文件(即一打开文件,文件指针自动移到文件末尾),如果文件不存在则创建
r+:以读写方式打开文件,可对文件进行读和写操作。
w+:消除文件内容,然后以读写方式打开文件。
a+:以读写方式打开文件,并把文件指针移到文件尾。

2. 读取txt文件

# 读出文件,如果有count,则读出count个字节,如果不设count则读取整个文件。
f.read([count])  

# 读出一行信息。  
f.readline() 

# 读出所有行,也就是读出整个文件的信息。   
f.readlines()   

Python处理excel与txt文件详解

f = open(r"f:\test.txt", "r")
print(f.read(5))
f.close()

输出:1 2 3

f = open(r"f:\test.txt", "r")
print(f.readline())
print(f.readline())
f.close()

输出
1 2 3 4 5
6,7,8,9,10

f = open(r"f:\test.txt", "r")
print(f.readlines())
f.close()

输出:[‘1 2 3 4 5\n', ‘6,7,8,9,10\n']

上述读取的格式均为:str 类型

3. 写入txt文件(需注意别清空了原来的内容)

首先指定待写入的文件,注意这里是 ‘w'

f = open(r'f:\test.txt','w')
f.write('hello world!')
f.close()

Python处理excel与txt文件详解

content = ['\nhello world1!','\nhello world2!','\nhello world3!\n']
f = open(r'f:\test.txt','w')
f.writelines(content)
f.close()

Python处理excel与txt文件详解

到此这篇关于python处理excel与txt文件详解的文章就介绍到这了,更多相关python处理excel与txt内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关标签: Python excel txt