python 将MySQL表导出为.csv

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 将MySQL表导出为.csv相关的知识,希望对你有一定的参考价值。

"""Exports MySQL tables to CSV"""

import os

import pymysql
import pandas as pd


HOST = 'database_host_here'
DATABASE = 'database_name_here'
USER = 'mysql_username_here'
PASSWORD = 'your_password_here'
DESTINATION = 'destination_directory_here'


def ensure_dirs(path):
    if not os.path.exists(path):
        os.makedirs(path)


def table_names_query():
    return """SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
              WHERE TABLE_SCHEMA = '{}'""".format(DATABASE)


def column_names_query(table_name):
    return """SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
              WHERE TABLE_SCHEMA = '{0}'
              AND TABLE_NAME = '{1}'""".format(DATABASE, table_name)


def column_names(cursor, table_name):
    cursor.execute(column_names_query(table_name))
    return [tup[0] for tup in list(cursor.fetchall())]


def table_names(cursor):
    cursor.execute(table_names_query())
    return [tup[0] for tup in list(cursor.fetchall())]


def chunks_to_csv(chunks, filepath):
    header = True
    for chunk in chunks:
        chunk.to_csv(filepath, mode='a', header=header,
                     index=False, encoding='utf-8')
        header = False


def main():
    ensure_dirs(DESTINATION)
    conn = pymysql.connect(host=HOST, user=USER, passwd=PASSWORD, db=DATABASE)
    cursor = conn.cursor()
    query = 'SELECT * FROM {table_name}'
    for table_name in table_names(cursor):
        chunks = pd.read_sql(query.format(table_name=table_name), conn,
                             columns=column_names(cursor, table_name),
                             chunksize=1000)
        filepath = os.path.join(DESTINATION, ''.join([table_name, '.csv']))
        if not os.path.exists(filepath):
            chunks_to_csv(chunks, filepath)
    cursor.close()
    conn.close()


if __name__ == "__main__":
    main()

以上是关于python 将MySQL表导出为.csv的主要内容,如果未能解决你的问题,请参考以下文章

使用 PHP/PDO 将大型 MySQL 表导出为 CSV

将 MySQL 导出为 CSV,一些列带引号,一些不带引号

Python 多张表导出为 csv

将 mysql 表导出到 csv 中,其中数据在列中包含换行符

MySQL:将多个表导出到 CSV 文件

将 JSON 数据从 MySQL 表导出到 CSV