使用Python读取表格中的某一行数据
import pandas as pd
file_path = 'C:\Users\EDY\PJ-IPAStudio\designer\project\导入项目PUvNit.xlsx'
def get_header_as_array(file_path):
try:
# 使用 pandas 读取 Excel 文件
df = pd.read_excel(file_path, header=None, nrows=1) # 只读取第一行
# 将 pandas Series 转换为列表
header_array = df.iloc[0].tolist()
return header_array
except FileNotFoundError:
print(f"文件 {file_path} 未找到。")
return []
except Exception as e:
print(f"读取文件时发生错误: {e}")
return []
headers = get_header_as_array(file_path)
print(headers)
pd.read_excel(file_path, header=None, nrows=1)
第一个参数是文件路径,header=None 表示会读取第一行,不添加这个参数的话,会跳过第一行(默认认为第一行为表头),nrows=1 读取一行的数据
df.iloc[0].tolist()
df.iloc[0]表示从下标为0的地方获取,tolist()转换为一个 Python 列表
df.iloc[0,1:] 表示从0行的地方获取,第二个数据,二维数组
原文地址:https://blog.csdn.net/weixin_51722520/article/details/139867451
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!