最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用Python處理Excel文件并將數(shù)據(jù)存儲到PostgreSQL的方法

 更新時間:2024年01月24日 10:02:48   作者:一只寫程序的猿  
在日常工作中,我們經(jīng)常會遇到需要處理大量文件并將數(shù)據(jù)存儲至數(shù)據(jù)庫或整合到一個文件的需求,本文將向大家展示如何使用Python處理Excel文件并將數(shù)據(jù)存儲到PostgreSQL數(shù)據(jù)庫中,需要的朋友可以參考下

在日常工作中,我們經(jīng)常會遇到需要處理大量文件并將數(shù)據(jù)存儲至數(shù)據(jù)庫或整合到一個文件的需求。這個任務(wù)對于人力和時間來說都是一大挑戰(zhàn)。幸運(yùn)的是,我們有Python這個神奇的工具,可以幫助我們自動化這個任務(wù),省時又省力!現(xiàn)在,我將向你展示如何使用Python處理Excel文件并將數(shù)據(jù)存儲到PostgreSQL數(shù)據(jù)庫中。

先打個底:以理解為主,不夠嚴(yán)謹(jǐn),如果看完還是不會,那一定是我講的不夠好,千萬別影響你們探索Python的興趣。

在我們的奇妙冒險中,如果你想將多個excel文件整合到一個表中,需要滿足一個前置條件——每個excel文件的格式和列對應(yīng)的含義順序必須一致。但是,如果表頭不一樣也沒關(guān)系,我們可以用程序來解決這個問題。本文將帶你進(jìn)入Python的魔法世界,教你如何處理Excel文件并將數(shù)據(jù)存儲到PostgreSQL數(shù)據(jù)庫中。在開始之前,我們需要安裝一些神奇的庫:

  • pandas:用于處理Excel文件中的數(shù)據(jù)
  • sqlalchemy:用于連接和操作PostgreSQL數(shù)據(jù)庫 安裝方法這里就不再重點講了了,直接搜網(wǎng)上的教程安裝即可。 ###1.日志記錄 開局先送送你一串Python日志記錄的代碼,可在任何場景下復(fù)用,它能夠?qū)崟r監(jiān)測程序的運(yùn)行狀態(tài),輕松解決測試和問題排查的難題。

注意:log_home需要改為自己本地路徑

# 定義日志記錄器
log_home = '/home/xusl/log/excel'  # 請將此路徑改為你自己的本地路徑
log_level = logging.INFO
log_to_console = True
log_config = {
    'version': 1,
    'formatters': {
        'generic': {
            'format': '%(asctime)s %(levelname)-5.5s [%(name)s:%(lineno)s][%(threadName)s] %(message)s',
        },
        'simple': {
            'format': '%(asctime)s %(levelname)-5.5s %(message)s',
        },
    },
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'formatter': 'generic',
        },
        'file': {
            'class': 'logging.FileHandler',
            'filename': os.path.join(log_home, 'excel_to_data.log'),
            'encoding': 'utf-8',
            'formatter': 'generic',

        },
    },
    'root': {
        'level': log_level,
        'handlers': ['console', 'file', ] if log_to_console else ['file', ],
    }
}
logging.config.dictConfig(log_config)
logger = logging.getLogger(__name__)

1.數(shù)據(jù)庫連接

接下來,我們需要配置自己的數(shù)據(jù)庫信息。

# 建立與PostgreSQL數(shù)據(jù)庫的連接 此處需要更改為自己的數(shù)據(jù)庫配置
db_user = 'dps'
db_password = 'DPS888'
db_host = '10.12.8.88'
db_port = '5432'
db_name = 'dpstest'


def get_conn():
    conn_url = 'postgresql+psycopg2://{user}:{password}@{host}:{port}/{database}'
    engine = create_engine(conn_url.format(database=db_name, user=db_user, password=db_password, host=db_host, port=db_port),
                                 pool_size=20,
                                 pool_recycle=7200,
                                 connect_args={'connect_timeout': 30})
    try:
        with engine.connect():
            logger.info('成功連接到數(shù)據(jù)庫')
    except Exception as e:
        logger.error('無法連接到數(shù)據(jù)庫:', str(e))
    return engine

2.設(shè)計及創(chuàng)建表結(jié)構(gòu)

根據(jù)文件內(nèi)容來設(shè)計和創(chuàng)建表結(jié)構(gòu),當(dāng)然你也可以用中文

# 創(chuàng)建存儲數(shù)據(jù)的表
table_name = 'public.excel_data'
ddl = """
DROP TABLE IF EXISTS public.excel_data;
CREATE TABLE IF NOT EXISTS public.excel_data (
    file_nm VARCHAR(255),
    cust_nm VARCHAR(255),
    cert_no VARCHAR(255),
    prod_nm VARCHAR(255),
    amt numeric(20,2),
    crt_dtm timestamp NOT NULL DEFAULT now() -- 創(chuàng)建時間
);
"""

3.處理數(shù)據(jù)

思路如下:

  • 提取文件名

  • 讀取Excel文件數(shù)據(jù)并提取前4列

  • 列名重命名

  • 根據(jù)條件過濾末尾的空行

  • 將數(shù)據(jù)存儲到PostgreSQL表中

  • 處理成功后將Excel文件移動到end目錄

重點講下to_sql()函數(shù):

  • name:SQL 表名

  • con:與數(shù)據(jù)庫鏈接的?式,推薦使?sqlalchemy的engine類型

  • schema:相應(yīng)數(shù)據(jù)庫的引擎,不設(shè)置則使?數(shù)據(jù)庫的默認(rèn)引擎,如mysql中的innodb引擎

  • if_exists:當(dāng)數(shù)據(jù)庫中已經(jīng)存在數(shù)據(jù)表時對數(shù)據(jù)表的操作,有replace替換、append追加,fail則當(dāng)表存在時提?

  • index:對DataFrame的index索引的處理,為True時索引也將作為數(shù)據(jù)寫?數(shù)據(jù)表

  • index_label:當(dāng)上?個參數(shù)index為True時,設(shè)置寫?數(shù)據(jù)表時index的列名稱

  • chunsize:設(shè)置整數(shù),如20000,?次寫?數(shù)據(jù)時的數(shù)據(jù)?數(shù)量,當(dāng)數(shù)據(jù)量很?時,需要設(shè)置,否則會鏈接超時寫?失敗。

  • dtype:列名到 SQL 類型的字典,默認(rèn)無;可選地指定列的數(shù)據(jù)類型

完整代碼如下:

import os
import pandas as pd
import logging.config
import shutil
import datetime

from sqlalchemy import create_engine

_tb_nm = 'excel_to_data'
_tb_nm_cn = "excel數(shù)據(jù)入庫"
_service_code = _tb_nm
# 日志目錄
log_home = '/home/xusl/log/excel'

# 日志level
log_level = logging.INFO

# 日志打印到控制臺
log_to_console = True

# 配置日志記錄器
log_config = {
    'version': 1,
    'formatters': {
        'generic': {
            'format': '%(asctime)s %(levelname)-5.5s [%(name)s:%(lineno)s][%(threadName)s] %(message)s',
        },
        'simple': {
            'format': '%(asctime)s %(levelname)-5.5s %(message)s',
        },
    },
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'formatter': 'generic',
        },
        'file': {
            'class': 'logging.FileHandler',
            'filename': os.path.join(log_home, _tb_nm + '.log'),
            'encoding': 'utf-8',
            'formatter': 'generic',

        },
    },
    'root': {
        'level': log_level,
        'handlers': ['console', 'file', ] if log_to_console else ['file', ],
    }
}
logging.config.dictConfig(log_config)
logger = logging.getLogger(_tb_nm)


# 建立與PostgreSQL數(shù)據(jù)庫的連接 39數(shù)據(jù)庫
db_user = 'dps'
db_password = 'DPS888'
db_host = '10.12.8.88'
db_port = '5432'
db_name = 'dpstest'



def get_conn():
    conn_url = 'postgresql+psycopg2://{user}:{password}@{host}:{port}/{database}'
    engine = create_engine(conn_url.format(database=db_name, user=db_user, password=db_password, host=db_host, port=db_port),
                                 pool_size=20,
                                 pool_recycle=7200,
                                 connect_args={'connect_timeout': 30})
    try:
        with engine.connect():
            print('成功連接到數(shù)據(jù)庫')
    except Exception as e:
        print('無法連接到數(shù)據(jù)庫:', str(e))
    return engine

# engine = create_engine(f'postgresql://{db_user}:{db_password}@{db_host}:{db_port}/{db_name}')


# 創(chuàng)建存儲數(shù)據(jù)的表
table_name = 'public.excel_data'

ddl = """
DROP TABLE IF EXISTS public.excel_data;
CREATE TABLE IF NOT EXISTS public.excel_data (
    file_nm VARCHAR(255),
    cust_nm VARCHAR(255),
    cert_no VARCHAR(255),
    prod_nm VARCHAR(255),
    amt numeric(20,2),
    crt_dtm timestamp NOT NULL DEFAULT now() -- 創(chuàng)建時間
);
"""


# 遍歷指定目錄下的所有Excel文件
excel_dir = '/home/xusl/data'
src_excel = '/home/xusl/data/src'
end_excel = '/home/xusl/data/end'
src_dir = 'src'
end_dir = 'end'


def deal(conn):
    for filename in os.listdir(src_excel):
        if not filename.endswith('.xlsx'):
            logging.info('沒有excel文件!')
            continue
        else:
            logging.info('')
            logging.info('')
            excel_file = os.path.join(src_excel, filename)

            # 提取文件名
            file_nm = os.path.basename(excel_file)
            func_name = file_nm
            logging.info('start %s' % func_name)
            logging.info(f'Reading data from {excel_file}')
            d0 = datetime.datetime.now()

            # 讀取Excel文件數(shù)據(jù)并提取前4列
            try:
                df = pd.read_excel(excel_file, usecols=[0, 1, 2, 3])
                logging.info('df讀取內(nèi)容:%s ' % df)
            except Exception as e:
                logging.error(f'Error reading file {excel_file}: {str(e)}')
                continue

            # 修改列名
            df.columns = ['cust_nm', 'cert_no', 'prod_nm', 'amt']
            logging.info('df修改后內(nèi)容:%s ' % df)

            # 根據(jù)條件過濾末尾的空行
            if not df.empty and df.iloc[-1].isnull().all():
                df = df[:-1]
                logging.debug('df刪減末尾后:%s ' % df)

            # 將數(shù)據(jù)存儲到PostgreSQL表中
            df['file_nm'] = file_nm
            df = df[['file_nm', 'cust_nm', 'cert_no', 'prod_nm', 'amt']]

            try:
                # 將整個DF導(dǎo)入數(shù)據(jù)庫中
                df.to_sql(name='excel_data', schema='public', con=conn, if_exists="append", index=False)
                d1 = datetime.datetime.now()
                s = (d1 - d0).total_seconds()
                logging.info('... end %s, 耗時: %s 秒. ' % (func_name, s))
            except Exception as e:
                logging.error(f'Error inserting data from file {excel_file}: {str(e)}')
                continue

            # 處理成功后將Excel文件移動到end目錄
            src_file = os.path.join(src_excel, filename)
            end_file = os.path.join(end_excel, filename)
            try:
                shutil.move(src_file, end_file)
            except Exception as e:
                logging.error(f'Error moving file {src_file} to {end_file}: {str(e)}')

    # 關(guān)閉數(shù)據(jù)庫連接
    # engine.dispose()


if __name__ == '__main__':
    engine = get_conn()
    deal(engine)

以上就是使用Python處理Excel文件并將數(shù)據(jù)存儲到PostgreSQL的方法的詳細(xì)內(nèi)容,更多關(guān)于Python處理Excel文件的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python編程使用tkinter模塊實現(xiàn)計算器軟件完整代碼示例

    Python編程使用tkinter模塊實現(xiàn)計算器軟件完整代碼示例

    這篇文章主要介紹了Python編程實現(xiàn)一個計算器軟件完整代碼示例,簡單介紹了Tkinter的相關(guān)內(nèi)容,然后分享了通過tkinter模塊開發(fā)一個計算器的完整Python代碼,具有一定借鑒價值,需要的朋友可以參考下。
    2017-11-11
  • Django在Model保存前記錄日志實例

    Django在Model保存前記錄日志實例

    這篇文章主要介紹了Django在Model保存前記錄日志實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-05-05
  • Python的json.loads() 方法與json.dumps()方法及使用小結(jié)

    Python的json.loads() 方法與json.dumps()方法及使用小結(jié)

    json.loads() 是一個非常有用的方法,它允許你在處理 JSON 數(shù)據(jù)時,將其轉(zhuǎn)換為 Python 數(shù)據(jù)類型,以便于在代碼中進(jìn)行操作和處理,這篇文章給大家介紹Python的json.loads() 方法與json.dumps()方法及使用小結(jié),感興趣的朋友一起看看吧
    2024-03-03
  • Python實現(xiàn)的爬蟲功能代碼

    Python實現(xiàn)的爬蟲功能代碼

    這篇文章主要介紹了Python實現(xiàn)的爬蟲功能,涉及Python使用urllib2、BeautifulSoup模塊實現(xiàn)網(wǎng)頁源碼的獲取、解析等相關(guān)操作技巧,需要的朋友可以參考下
    2017-06-06
  • python?turtle繪制多邊形和跳躍和改變速度特效

    python?turtle繪制多邊形和跳躍和改變速度特效

    這篇文章主要介紹了python?turtle繪制多邊形和跳躍和改變速度特效,文章實現(xiàn)過程詳細(xì),需要的小伙伴可以參考一下,希望對你的學(xué)習(xí)有所幫助
    2022-03-03
  • Python 十六進(jìn)制整數(shù)與ASCii編碼字符串相互轉(zhuǎn)換方法

    Python 十六進(jìn)制整數(shù)與ASCii編碼字符串相互轉(zhuǎn)換方法

    今天小編就為大家分享一篇Python 十六進(jìn)制整數(shù)與ASCii編碼字符串相互轉(zhuǎn)換方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • Python super( )函數(shù)用法總結(jié)

    Python super( )函數(shù)用法總結(jié)

    今天給大家?guī)淼闹R是關(guān)于Python的相關(guān)知識,文章圍繞著super( )函數(shù)展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • 如何使用Python生成Hilbert矩陣

    如何使用Python生成Hilbert矩陣

    這篇文章主要介紹了如何使用Python生成Hilbert矩陣,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-09-09
  • Python利用物理引擎Pymunk編寫一個解壓小游戲

    Python利用物理引擎Pymunk編寫一個解壓小游戲

    這篇文章主要為大家詳細(xì)介紹了Python如何利用物理引擎Pymunk編寫一個解壓小游戲,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以嘗試一下
    2023-01-01
  • flask開啟多線程的具體方法

    flask開啟多線程的具體方法

    在本篇內(nèi)容里小編給大家整理的是一篇關(guān)于flask開啟多線程的具體方法,對此有需求的可以學(xué)習(xí)參考下。
    2020-08-08

最新評論

灌云县| 霍邱县| 宁安市| 房山区| 井研县| 宝兴县| 平顶山市| 肇庆市| 黑河市| 盘锦市| 巴东县| 宁安市| 江陵县| 怀集县| 得荣县| 个旧市| 嫩江县| 鄂尔多斯市| 新乡县| 台南市| 阜平县| 曲周县| 札达县| 东辽县| 泗阳县| 南安市| 花垣县| 泌阳县| 横峰县| 二连浩特市| 荥经县| 吉林市| 崇信县| 汕头市| 阿克陶县| 北宁市| 岳阳市| 麦盖提县| 佛冈县| 麻阳| 金湖县|