We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
def pd_excel_to_list(file_name): """ 用pandas读取excel文件,结合numpy将数据转为list :param file_name: 传入文件的路径 :return: [[], [] ,[]], 类似[['列1', '列2', '列3'], ['2行.1', '2行.2', '2行.3'], ['3行.1', '3行.2', '3行.3']] """ df = pd.read_excel(file_name, index=True, header=None) df = df.fillna('') # 将表格的nan填充为'' np_data = np.array(df) # pd.values.tolist()只能转换到值,用np将所有的tolist data_list = np_data.tolist() return data_list def pd_excel_to_list_of_dict(path): """ 将excel表格的数据转为字典 - 第一行作为key - 第二行与key对应的值作为value - 直到N行,没有数据 :param path: excel 表格的路径 :return: 类似[{'列1': '2行.1', '列2': '2行.2', '列3': '2行.3'}, {'列1': '3行.1', '列2': '3行.2', '列3': '3行.3'}] """ # 读取Excel文件,默认读取第一个sheet df = pd.read_excel(path) # 替换Excel表格内的空单元格,否则在下一步处理中将会报错 df.fillna("") # 调用pd_excel函数,读取的数据转为list d_list = pd_excel_to_list(path) # for循环,将表格数据转为字典 df_list = [] for i in df.index.values: # loc为按列名索引,iloc为按位置索引,使用的是 [[行号], [列名]] df_line = df.loc[i, d_list[0]].to_dict() # d_list[0]填入的是第一列的key # 将每一行转换成字典后添加到列表 df_list.append(df_line) return df_list
The text was updated successfully, but these errors were encountered:
No branches or pull requests
The text was updated successfully, but these errors were encountered: