{"id":1174959,"date":"2025-01-15T17:25:20","date_gmt":"2025-01-15T09:25:20","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1174959.html"},"modified":"2025-01-15T17:25:25","modified_gmt":"2025-01-15T09:25:25","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e8%ae%a1%e7%ae%97%e8%a1%a8%e6%80%bb%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1174959.html","title":{"rendered":"\u5982\u4f55\u7528python\u8ba1\u7b97\u8868\u603b\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25110546\/848a0fbd-ed82-4f4e-a750-d2ef71ad733c.webp\" alt=\"\u5982\u4f55\u7528python\u8ba1\u7b97\u8868\u603b\u6570\" \/><\/p>\n<p><p> \u8981\u7528Python\u8ba1\u7b97\u8868\u603b\u6570\uff0c\u4e3b\u8981\u7684\u65b9\u6cd5\u5305\u62ec<strong>\u4f7f\u7528pandas\u5e93\u3001\u76f4\u63a5\u8bfb\u53d6\u6587\u4ef6\u76ee\u5f55\u3001\u5229\u7528SQLAlchemy\u8fde\u63a5\u6570\u636e\u5e93<\/strong>\u7b49\u3002\u4e0b\u9762\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u4f7f\u7528pandas\u5e93\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528pandas\u5e93\uff1a<\/strong><\/p>\n<p>Pandas\u662fPython\u4e2d\u975e\u5e38\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u80fd\u591f\u65b9\u4fbf\u5730\u8bfb\u53d6\u548c\u5904\u7406\u5404\u79cd\u683c\u5f0f\u7684\u6570\u636e\u6587\u4ef6\u3002\u6211\u4eec\u53ef\u4ee5\u5229\u7528pandas\u5e93\u6765\u8bfb\u53d6Excel\u6587\u4ef6\u4e2d\u7684\u8868\u683c\uff0c\u5e76\u8ba1\u7b97\u8868\u683c\u603b\u6570\u3002<\/p>\n<\/p>\n<p><p>\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5\u5e76\u5bfc\u5165pandas\u5e93\u3002<\/li>\n<li>\u4f7f\u7528pandas\u4e2d\u7684<code>pd.ExcelFile()<\/code>\u65b9\u6cd5\u8bfb\u53d6Excel\u6587\u4ef6\u3002<\/li>\n<li>\u4f7f\u7528<code>sheet_names<\/code>\u5c5e\u6027\u83b7\u53d6\u6240\u6709\u8868\u683c\u7684\u540d\u79f0\u5217\u8868\u3002<\/li>\n<li>\u8ba1\u7b97\u8868\u683c\u603b\u6570\u3002<\/li>\n<\/ol>\n<p><p>\u63a5\u4e0b\u6765\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h3>\u5b89\u88c5\u5e76\u5bfc\u5165pandas\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86pandas\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u7740\uff0c\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165pandas\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f7f\u7528pandas\u4e2d\u7684<code>pd.ExcelFile()<\/code>\u65b9\u6cd5\u8bfb\u53d6Excel\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>pd.ExcelFile()<\/code>\u65b9\u6cd5\u8bfb\u53d6Excel\u6587\u4ef6\uff0c\u5e76\u5c06\u5176\u52a0\u8f7d\u4e3a\u4e00\u4e2aExcelFile\u5bf9\u8c61\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">excel_file = pd.ExcelFile(&#39;path_to_excel_file.xlsx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\u7684<code>&#39;path_to_excel_file.xlsx&#39;<\/code>\u662fExcel\u6587\u4ef6\u7684\u8def\u5f84\uff0c\u53ef\u4ee5\u662f\u76f8\u5bf9\u8def\u5f84\u6216\u7edd\u5bf9\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528<code>sheet_names<\/code>\u5c5e\u6027\u83b7\u53d6\u6240\u6709\u8868\u683c\u7684\u540d\u79f0\u5217\u8868<\/h3>\n<\/p>\n<p><p>ExcelFile\u5bf9\u8c61\u7684<code>sheet_names<\/code>\u5c5e\u6027\u5305\u542b\u4e86Excel\u6587\u4ef6\u4e2d\u6240\u6709\u8868\u683c\u7684\u540d\u79f0\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">sheet_names = excel_file.sheet_names<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u8ba1\u7b97\u8868\u683c\u603b\u6570<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u83b7\u53d6\u8868\u683c\u540d\u79f0\u5217\u8868\u7684\u957f\u5ea6\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5f97\u5230\u8868\u683c\u7684\u603b\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">total_sheets = len(sheet_names)<\/p>\n<p>print(f&#39;Total number of sheets: {total_sheets}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u8ba1\u7b97\u51faExcel\u6587\u4ef6\u4e2d\u7684\u8868\u683c\u603b\u6570\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528SQLAlchemy\u8fde\u63a5\u6570\u636e\u5e93\u5e76\u8ba1\u7b97\u8868\u603b\u6570\uff0c\u4ee5\u53ca\u5982\u4f55\u76f4\u63a5\u8bfb\u53d6\u6587\u4ef6\u76ee\u5f55\u6765\u8ba1\u7b97\u8868\u603b\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528SQLAlchemy\u8fde\u63a5\u6570\u636e\u5e93\u8ba1\u7b97\u8868\u603b\u6570<\/h3>\n<\/p>\n<p><p>SQLAlchemy\u662fPython\u4e2d\u975e\u5e38\u5f3a\u5927\u7684\u6570\u636e\u5e93\u8fde\u63a5\u5e93\uff0c\u652f\u6301\u591a\u79cd\u6570\u636e\u5e93\u7c7b\u578b\uff0c\u5305\u62ecSQLite\u3001MySQL\u3001PostgreSQL\u7b49\u3002\u6211\u4eec\u53ef\u4ee5\u5229\u7528SQLAlchemy\u8fde\u63a5\u6570\u636e\u5e93\uff0c\u5e76\u8ba1\u7b97\u6570\u636e\u5e93\u4e2d\u7684\u8868\u603b\u6570\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5\u5e76\u5bfc\u5165SQLAlchemy\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86SQLAlchemy\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pip install SQLAlchemy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u7740\uff0c\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165SQLAlchemy\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sqlalchemy import create_engine, inspect<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8fde\u63a5\u6570\u636e\u5e93<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528SQLAlchemy\u4e2d\u7684<code>create_engine()<\/code>\u65b9\u6cd5\u521b\u5efa\u6570\u636e\u5e93\u8fde\u63a5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">engine = create_engine(&#39;database_connection_string&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\u7684<code>&#39;database_connection_string&#39;<\/code>\u662f\u6570\u636e\u5e93\u7684\u8fde\u63a5\u5b57\u7b26\u4e32\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<ul>\n<li>SQLite: <code>&#39;sqlite:\/\/\/path_to_database.db&#39;<\/code><\/li>\n<li>MySQL: <code>&#39;mysql+pymysql:\/\/username:password@host:port\/database&#39;<\/code><\/li>\n<li>PostgreSQL: <code>&#39;postgresql:\/\/username:password@host:port\/database&#39;<\/code><\/li>\n<\/ul>\n<p><h4>\u83b7\u53d6\u6570\u636e\u5e93\u4e2d\u7684\u8868\u540d\u79f0\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528SQLAlchemy\u4e2d\u7684<code>inspect()<\/code>\u65b9\u6cd5\u83b7\u53d6\u6570\u636e\u5e93\u4e2d\u7684\u8868\u540d\u79f0\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">inspector = inspect(engine)<\/p>\n<p>table_names = inspector.get_table_names()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8ba1\u7b97\u8868\u603b\u6570<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u83b7\u53d6\u8868\u540d\u79f0\u5217\u8868\u7684\u957f\u5ea6\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5f97\u5230\u6570\u636e\u5e93\u4e2d\u7684\u8868\u603b\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">total_tables = len(table_names)<\/p>\n<p>print(f&#39;Total number of tables: {total_tables}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u76f4\u63a5\u8bfb\u53d6\u6587\u4ef6\u76ee\u5f55\u8ba1\u7b97\u8868\u603b\u6570<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528pandas\u5e93\u548cSQLAlchemy\u8fde\u63a5\u6570\u636e\u5e93\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u76f4\u63a5\u8bfb\u53d6\u6587\u4ef6\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\u5217\u8868\u6765\u8ba1\u7b97\u8868\u603b\u6570\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e3b\u8981\u9002\u7528\u4e8e\u76ee\u5f55\u4e2d\u5305\u542b\u591a\u4e2a\u8868\u683c\u6587\u4ef6\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528os\u5e93\u8bfb\u53d6\u6587\u4ef6\u76ee\u5f55<\/h4>\n<\/p>\n<p><p>Python\u4e2d\u7684os\u5e93\u63d0\u4f9b\u4e86\u8bfb\u53d6\u6587\u4ef6\u76ee\u5f55\u7684\u65b9\u6cd5\u3002\u6211\u4eec\u53ef\u4ee5\u5229\u7528os\u5e93\u6765\u8bfb\u53d6\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\u5217\u8868\uff0c\u5e76\u8ba1\u7b97\u8868\u683c\u6587\u4ef6\u7684\u603b\u6570\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165os\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u83b7\u53d6\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528os\u5e93\u7684<code>os.listdir()<\/code>\u65b9\u6cd5\u83b7\u53d6\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file_list = os.listdir(&#39;directory_path&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\u7684<code>&#39;directory_path&#39;<\/code>\u662f\u76ee\u5f55\u7684\u8def\u5f84\uff0c\u53ef\u4ee5\u662f\u76f8\u5bf9\u8def\u5f84\u6216\u7edd\u5bf9\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h4>\u8fc7\u6ee4\u8868\u683c\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u6587\u4ef6\u6269\u5c55\u540d\u8fc7\u6ee4\u76ee\u5f55\u4e2d\u7684\u8868\u683c\u6587\u4ef6\uff0c\u4f8b\u5982Excel\u6587\u4ef6\uff08.xlsx\u6216.xls\uff09\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">table_files = [file for file in file_list if file.endswith(&#39;.xlsx&#39;) or file.endswith(&#39;.xls&#39;)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8ba1\u7b97\u8868\u683c\u6587\u4ef6\u603b\u6570<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u83b7\u53d6\u8868\u683c\u6587\u4ef6\u5217\u8868\u7684\u957f\u5ea6\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5f97\u5230\u76ee\u5f55\u4e2d\u8868\u683c\u6587\u4ef6\u7684\u603b\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">total_table_files = len(table_files)<\/p>\n<p>print(f&#39;Total number of table files: {total_table_files}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u7684\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u8ba1\u7b97\u8868\u603b\u6570\uff0c\u5305\u62ec\u4f7f\u7528pandas\u5e93\u8bfb\u53d6Excel\u6587\u4ef6\u3001\u5229\u7528SQLAlchemy\u8fde\u63a5\u6570\u636e\u5e93\u4ee5\u53ca\u76f4\u63a5\u8bfb\u53d6\u6587\u4ef6\u76ee\u5f55\u6765\u8ba1\u7b97\u8868\u603b\u6570\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u6bcf\u79cd\u65b9\u6cd5\u7684\u5177\u4f53\u5b9e\u73b0\u4ee3\u7801\u53ca\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h2>\u4f7f\u7528pandas\u5e93\u8ba1\u7b97\u8868\u603b\u6570<\/h2>\n<\/p>\n<p><h3>\u4ee3\u7801\u5b9e\u73b0<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4f7f\u7528pandas\u5e93\u8ba1\u7b97Excel\u6587\u4ef6\u4e2d\u8868\u683c\u603b\u6570\u7684\u5b8c\u6574\u4ee3\u7801\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>def calculate_excel_sheets(file_path):<\/p>\n<p>    # \u8bfb\u53d6Excel\u6587\u4ef6<\/p>\n<p>    excel_file = pd.ExcelFile(file_path)<\/p>\n<p>    # \u83b7\u53d6\u6240\u6709\u8868\u683c\u7684\u540d\u79f0\u5217\u8868<\/p>\n<p>    sheet_names = excel_file.sheet_names<\/p>\n<p>    # \u8ba1\u7b97\u8868\u683c\u603b\u6570<\/p>\n<p>    total_sheets = len(sheet_names)<\/p>\n<p>    return total_sheets<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>file_path = &#39;example.xlsx&#39;<\/p>\n<p>total_sheets = calculate_excel_sheets(file_path)<\/p>\n<p>print(f&#39;Total number of sheets in {file_path}: {total_sheets}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528pandas\u5e93\u8ba1\u7b97\u8868\u603b\u6570\u9002\u7528\u4e8e\u4ee5\u4e0b\u573a\u666f\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u9700\u8981\u5904\u7406\u5355\u4e2a\u6216\u591a\u4e2aExcel\u6587\u4ef6\u4e2d\u7684\u8868\u683c\u3002<\/li>\n<li>\u9700\u8981\u8fdb\u4e00\u6b65\u5904\u7406\u548c\u5206\u6790Excel\u6587\u4ef6\u4e2d\u7684\u6570\u636e\uff0c\u4f8b\u5982\u6570\u636e\u6e05\u6d17\u3001\u6570\u636e\u8f6c\u6362\u7b49\u3002<\/li>\n<\/ol>\n<p><h2>\u4f7f\u7528SQLAlchemy\u8fde\u63a5\u6570\u636e\u5e93\u8ba1\u7b97\u8868\u603b\u6570<\/h2>\n<\/p>\n<p><h3>\u4ee3\u7801\u5b9e\u73b0<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4f7f\u7528SQLAlchemy\u8fde\u63a5\u6570\u636e\u5e93\u5e76\u8ba1\u7b97\u8868\u603b\u6570\u7684\u5b8c\u6574\u4ee3\u7801\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sqlalchemy import create_engine, inspect<\/p>\n<p>def calculate_database_tables(connection_string):<\/p>\n<p>    # \u521b\u5efa\u6570\u636e\u5e93\u8fde\u63a5<\/p>\n<p>    engine = create_engine(connection_string)<\/p>\n<p>    # \u83b7\u53d6\u6570\u636e\u5e93\u4e2d\u7684\u8868\u540d\u79f0\u5217\u8868<\/p>\n<p>    inspector = inspect(engine)<\/p>\n<p>    table_names = inspector.get_table_names()<\/p>\n<p>    # \u8ba1\u7b97\u8868\u603b\u6570<\/p>\n<p>    total_tables = len(table_names)<\/p>\n<p>    return total_tables<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>connection_string = &#39;sqlite:\/\/\/example.db&#39;<\/p>\n<p>total_tables = calculate_database_tables(connection_string)<\/p>\n<p>print(f&#39;Total number of tables in the database: {total_tables}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528SQLAlchemy\u8fde\u63a5\u6570\u636e\u5e93\u8ba1\u7b97\u8868\u603b\u6570\u9002\u7528\u4e8e\u4ee5\u4e0b\u573a\u666f\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u9700\u8981\u5904\u7406\u548c\u7ba1\u7406\u6570\u636e\u5e93\u4e2d\u7684\u8868\u683c\u3002<\/li>\n<li>\u9700\u8981\u8fdb\u4e00\u6b65\u64cd\u4f5c\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\uff0c\u4f8b\u5982\u6570\u636e\u67e5\u8be2\u3001\u6570\u636e\u63d2\u5165\u7b49\u3002<\/li>\n<li>\u652f\u6301\u591a\u79cd\u6570\u636e\u5e93\u7c7b\u578b\uff0c\u5305\u62ecSQLite\u3001MySQL\u3001PostgreSQL\u7b49\u3002<\/li>\n<\/ol>\n<p><h2>\u76f4\u63a5\u8bfb\u53d6\u6587\u4ef6\u76ee\u5f55\u8ba1\u7b97\u8868\u603b\u6570<\/h2>\n<\/p>\n<p><h3>\u4ee3\u7801\u5b9e\u73b0<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u76f4\u63a5\u8bfb\u53d6\u6587\u4ef6\u76ee\u5f55\u5e76\u8ba1\u7b97\u8868\u683c\u6587\u4ef6\u603b\u6570\u7684\u5b8c\u6574\u4ee3\u7801\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def calculate_directory_table_files(directory_path):<\/p>\n<p>    # \u83b7\u53d6\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\u5217\u8868<\/p>\n<p>    file_list = os.listdir(directory_path)<\/p>\n<p>    # \u8fc7\u6ee4\u8868\u683c\u6587\u4ef6<\/p>\n<p>    table_files = [file for file in file_list if file.endswith(&#39;.xlsx&#39;) or file.endswith(&#39;.xls&#39;)]<\/p>\n<p>    # \u8ba1\u7b97\u8868\u683c\u6587\u4ef6\u603b\u6570<\/p>\n<p>    total_table_files = len(table_files)<\/p>\n<p>    return total_table_files<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>directory_path = &#39;tables_directory&#39;<\/p>\n<p>total_table_files = calculate_directory_table_files(directory_path)<\/p>\n<p>print(f&#39;Total number of table files in {directory_path}: {total_table_files}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u76f4\u63a5\u8bfb\u53d6\u6587\u4ef6\u76ee\u5f55\u8ba1\u7b97\u8868\u603b\u6570\u9002\u7528\u4e8e\u4ee5\u4e0b\u573a\u666f\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u76ee\u5f55\u4e2d\u5305\u542b\u591a\u4e2a\u8868\u683c\u6587\u4ef6\uff0c\u9700\u8981\u8ba1\u7b97\u8868\u683c\u6587\u4ef6\u7684\u603b\u6570\u3002<\/li>\n<li>\u9700\u8981\u8fdb\u4e00\u6b65\u5904\u7406\u548c\u5206\u6790\u76ee\u5f55\u4e2d\u7684\u8868\u683c\u6587\u4ef6\uff0c\u4f8b\u5982\u6587\u4ef6\u8bfb\u53d6\u3001\u6587\u4ef6\u5206\u7c7b\u7b49\u3002<\/li>\n<\/ol>\n<p><h2>\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u8be6\u7ec6\u4ecb\u7ecd\u548c\u4ee3\u7801\u5b9e\u73b0\uff0c\u6211\u4eec\u53ef\u4ee5\u6e05\u6670\u5730\u770b\u5230\u4f7f\u7528Python\u8ba1\u7b97\u8868\u603b\u6570\u7684\u591a\u79cd\u65b9\u6cd5\u3002\u6839\u636e\u5b9e\u9645\u9700\u6c42\u548c\u5e94\u7528\u573a\u666f\uff0c\u53ef\u4ee5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u8ba1\u7b97\u8868\u603b\u6570\uff0c\u5e76\u8fdb\u4e00\u6b65\u5904\u7406\u548c\u5206\u6790\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u662f\u5904\u7406\u5355\u4e2a\u6216\u591a\u4e2aExcel\u6587\u4ef6\u3001\u7ba1\u7406\u6570\u636e\u5e93\u4e2d\u7684\u8868\u683c\uff0c\u8fd8\u662f\u8bfb\u53d6\u6587\u4ef6\u76ee\u5f55\u4e2d\u7684\u8868\u683c\u6587\u4ef6\uff0cPython\u90fd\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5e93\u548c\u5de5\u5177\u6765\u5e2e\u52a9\u6211\u4eec\u9ad8\u6548\u5730\u5b8c\u6210\u4efb\u52a1\u3002\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u80fd\u591f\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u8ba1\u7b97\u8868\u603b\u6570\uff0c\u5e76\u89e3\u51b3\u5b9e\u9645\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u7528Python\u8ba1\u7b97Excel\u8868\u683c\u4e2d\u7684\u603b\u6570\uff1f<\/strong><br \/>\u4f7f\u7528Python\u5904\u7406Excel\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u5229\u7528<code>pandas<\/code>\u5e93\u3002\u9996\u5148\u5b89\u88c5\u8be5\u5e93\uff0c\u7136\u540e\u4f7f\u7528<code>read_excel<\/code>\u51fd\u6570\u8bfb\u53d6\u6587\u4ef6\uff0c\u63a5\u7740\u4f7f\u7528<code>sum()<\/code>\u65b9\u6cd5\u8ba1\u7b97\u6240\u9700\u5217\u7684\u603b\u548c\u3002\u4f8b\u5982\uff0c<code>df[&#39;\u5217\u540d&#39;].sum()<\/code>\u53ef\u4ee5\u8ba1\u7b97\u6307\u5b9a\u5217\u7684\u603b\u6570\u3002<\/p>\n<p><strong>Python\u4e2d\u5982\u4f55\u8ba1\u7b97\u5217\u8868\u6216\u6570\u7ec4\u7684\u603b\u548c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>sum()<\/code>\u51fd\u6570\u6765\u8ba1\u7b97\u5217\u8868\u6216\u6570\u7ec4\u7684\u603b\u548c\u3002\u53ea\u9700\u5c06\u5217\u8868\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9<code>sum()<\/code>\uff0c\u4f8b\u5982<code>total = sum(my_list)<\/code>\uff0c\u5373\u53ef\u83b7\u5f97\u603b\u548c\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u7528Python\u8ba1\u7b97\u6570\u636e\u5e93\u8868\u4e2d\u7684\u603b\u6570\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\u3002\u4f7f\u7528<code>sqlite3<\/code>\u6216<code>SQLAlchemy<\/code>\u7b49\u5e93\uff0c\u60a8\u53ef\u4ee5\u8fde\u63a5\u5230\u6570\u636e\u5e93\u5e76\u6267\u884cSQL\u67e5\u8be2\u6765\u83b7\u53d6\u603b\u6570\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>SELECT COUNT(*) FROM \u8868\u540d<\/code>\u7684\u67e5\u8be2\u8bed\u53e5\u53ef\u4ee5\u8fd4\u56de\u8868\u4e2d\u7684\u884c\u6570\uff0c\u4f7f\u7528Python\u7684\u6570\u636e\u5e93\u8fde\u63a5\u5e93\u6267\u884c\u6b64\u67e5\u8be2\u5373\u53ef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u7528Python\u8ba1\u7b97\u8868\u603b\u6570\uff0c\u4e3b\u8981\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528pandas\u5e93\u3001\u76f4\u63a5\u8bfb\u53d6\u6587\u4ef6\u76ee\u5f55\u3001\u5229\u7528SQLAlchemy\u8fde\u63a5\u6570 [&hellip;]","protected":false},"author":3,"featured_media":1174971,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1174959"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1174959"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1174959\/revisions"}],"predecessor-version":[{"id":1174977,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1174959\/revisions\/1174977"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1174971"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1174959"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1174959"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1174959"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}