当前位置: 首页 > 工具软件 > WriteExcel > 使用案例 >

17.python pandas 读写excel

谭仰岳
2023-12-01

author = ‘lzc’

-- coding: UTF-8 --

import os
import pandas as pd
import datetime
import time
from collections import OrderedDict

def read_excel(path):
#读取excel文件,返回一个标头list和values二维list
df = pd.read_excel(path)
# print (df)
data_head=df.columns.tolist()
data_value=df.values

return data_head,data_value

def write_excel(path,head,data):
#将list写入excel

df = pd.DataFrame(data,columns=head)
df.to_excel(path,index=False,header=True)
print ('写excel完成!')

def split_url(url):
#分离文件和拓展名
file,ext = os.path.splitext(url)
# print (file)
# print (ext)
return file,ext

if name ==“main”:

#读取文件路径,放在项目下
path ='Fastdfs中间件.xlsx'
data_head,data_value=read_excel(path)

listdate=[] #收集数据list
for list in data_value:
    if '10.92.168.3' in list:
        listdate.append(list)

file,ext=split_url(path)
write_path=file+"_select.xlsx"
write_excel(write_path,data_head,listdate)
 类似资料: