import pandas as pd
f=r'e:\bz.xlsx'
df = pd.read_excel(f)
# print(df[:2])
# print(df[:1])
# print(df[0:0])
# bt=df[:0]
print(df.index.values)
df_list = []
for i in df.index.values:
# loc为按列名索引 iloc 为按位置索引,使用的是 [[行号], [列名]]
df_line = df.loc[i, df.columns].to_dict()
# 将每一行转换成字典后添加到列表
df_list.append(df_line)
print(df_list[0])
print(df_list[0].keys())
shjk={'name':'类名','item':'产品名称'}
def getdata(shjk,list):
zd={}
for x in shjk.keys():
print(shjk[x])
print(list[0].keys())
if shjk[x] in list[0].keys():
zd[x]='row['+shjk[x]+']'
else:
zd[x]=''
return zd
gd=getdata(shjk,df_list)
print(gd)
def isrow( hz, row):
if hz in row.keys():
return row[hz]
else:
return ''
print(isrow('类名',df_list[0]))
print(isrow('类',df_list[0]))
python 试试pandas 读取excel功能
最新推荐文章于 2024-09-20 19:09:00 发布