python提取特定格式的數(shù)據(jù)的操作方法
本文檔總結(jié)了 ExcelGridConverter.py
腳本所涉及的關(guān)鍵 Python 知識點。該腳本用于從多個 Excel 文件中提取特定格式的數(shù)據(jù)并轉(zhuǎn)換為一個新的 Excel 文件。
導(dǎo)入庫
腳本使用了以下主要庫:
tkinter
:用于創(chuàng)建圖形用戶界面。pandas
:用于處理 Excel 數(shù)據(jù)。os
:用于處理文件和目錄路徑。
import tkinter as tk from tkinter import filedialog, messagebox import pandas as pd import os
Pandas 數(shù)據(jù)處理
讀取 Excel 文件
使用 pd.read_excel
方法讀取 Excel 文件,并使用 sheet_name=None
參數(shù)讀取所有工作表。添加 index_col=None
參數(shù)以確保第一列不會被自動設(shè)置為索引列。
source_df = pd.read_excel(file_path, sheet_name=None, index_col=None) source_data = source_df['一格一案']
數(shù)據(jù)提取
通過 Pandas 的 iloc
方法,根據(jù)行列索引提取特定數(shù)據(jù)。
result_data = { '網(wǎng)格編號': source_data.iloc[1, 1], '責(zé)任段': source_data.iloc[1, 3], ... }
處理合并單元格數(shù)據(jù):
risk_check_path = "\n".join(source_data.iloc[9:19, 1].dropna().astype(str)) result_data['五、風(fēng)險項點檢查路徑'] = risk_check_path
創(chuàng)建 DataFrame 并導(dǎo)出為 Excel 文件
將所有提取的數(shù)據(jù)放入一個 DataFrame 中,并使用 to_excel
方法導(dǎo)出為 Excel 文件。
result_df = pd.DataFrame(all_data) result_df.to_excel(output_file_path, index=False)
Tkinter GUI 界面
創(chuàng)建主窗口
使用 tk.Tk
創(chuàng)建主窗口,并設(shè)置窗口標(biāo)題、大小和位置。
root = tk.Tk() root.title("Excel 轉(zhuǎn)換工具") root.geometry(f'{window_width}x{window_height}+{position_right}+{position_top}')
創(chuàng)建按鈕和標(biāo)簽
使用 tk.Button
和 tk.Label
創(chuàng)建按鈕和標(biāo)簽,并設(shè)置其屬性和布局。
title_label = tk.Label(root, text="Excel 轉(zhuǎn)換工具", font=("Arial", 18)) title_label.pack(pady=20) select_button = tk.Button(root, text="選擇 Excel 文件", command=select_files, font=("Arial", 12)) select_button.pack(pady=10)
文件操作
文件對話框
使用 filedialog.askopenfilenames
打開文件選擇對話框,允許用戶選擇多個 Excel 文件。使用 filedialog.asksaveasfilename
打開文件保存對話框,允許用戶選擇保存路徑。
file_paths = filedialog.askopenfilenames(filetypes=[("Excel 文件", "*.xlsx")]) output_file_path = filedialog.asksaveasfilename(defaultextension=".xlsx", filetypes=[("Excel 文件", "*.xlsx")])
主要函數(shù)解釋
transform_to_result_format_specific
該函數(shù)從源數(shù)據(jù)中提取特定字段,并返回一個字典格式的結(jié)果數(shù)據(jù)。
def transform_to_result_format_specific(source_data, source_file_path): risk_check_path = "\n".join(source_data.iloc[9:19, 1].dropna().astype(str)) result_data = { ... } return result_data
select_files
該函數(shù)處理文件選擇、數(shù)據(jù)轉(zhuǎn)換和結(jié)果保存的主要邏輯。
def select_files(): file_paths = filedialog.askopenfilenames(filetypes=[("Excel 文件", "*.xlsx")]) all_data = [] for file_path in file_paths: source_df = pd.read_excel(file_path, sheet_name=None, index_col=None) source_data = source_df['一格一案'] transformed_data = transform_to_result_format_specific(source_data, file_path) all_data.append(transformed_data) result_df = pd.DataFrame(all_data) output_file_path = filedialog.asksaveasfilename(defaultextension=".xlsx", filetypes=[("Excel 文件", "*.xlsx")]) if output_file_path: result_df.to_excel(output_file_path, index=False) messagebox.showinfo("成功", "文件已成功轉(zhuǎn)換并保存。")
總結(jié)
通過本腳本,我們學(xué)習(xí)了如何使用 Pandas 讀取和處理 Excel 數(shù)據(jù),如何使用 Tkinter 創(chuàng)建圖形用戶界面,以及如何處理文件對話框和文件操作。這些知識點在日常的 Python 開發(fā)中非常實用,特別是涉及數(shù)據(jù)處理和用戶界面的項目中。
到此這篇關(guān)于python提取特定格式的數(shù)據(jù)的文章就介紹到這了,更多相關(guān)python提取數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
pygame+opencv實現(xiàn)讀取視頻幀的方法示例
由于pygame.movie.Movie.play()只支持MPEG格式的視頻,所以決定使用與opencv讀取視頻幀的畫面,本文就詳細(xì)的介紹了pygame+opencv實現(xiàn)讀取視頻幀,感興趣的可以了解一下2021-12-12Python實現(xiàn)定制自動化業(yè)務(wù)流量報表周報功能【XlsxWriter模塊】
這篇文章主要介紹了Python實現(xiàn)定制自動化業(yè)務(wù)流量報表周報功能,結(jié)合實例形式分析了Python基于XlsxWriter模塊操作xlsx文件生成報表圖的相關(guān)操作技巧,需要的朋友可以參考下2019-03-03