LoginSignup
1
1

More than 1 year has passed since last update.

【Python】cp932 の 大容量ファイルをイイカンジに取り込みたい

Last updated at Posted at 2022-07-18

cp932 の 大容量ファイルをイイカンジに取り込みたい

動作環境

  • Windows10 21H2
    • メモリ4GB
  • Python3.9

大容量ってどこまで?

1GBくらいまでならいけました。
なお、CSVをインポートしたSQLite3 も1GBくらいになりましたが
問題なく接続できました。

ソースコード


import codecs
import pandas as pd
import sqlite3 as sq
import datetime

def db_migration(file_path,db_file_path):
  try:
    dt_now = datetime.datetime.now()
    print(dt_now)
    column_names = ["column{0}".format(num) for num in range(255)]
    column_data_type = {}

    for column in column_names:
      column_data_type[column] = str
    
    # CSVを開く
    print('CSV Open')
    with codecs.open(file_path,"r","cp932","ignore") as f:
      df_csv = pd.read_csv(f,header=None,names=column_names,quoting=1,converters=column_data_type)

    # CSV <-> SQLite3
    print('SQLite Open')
    conn = sq.connect(db_file_path)
    cur = conn.cursor()
    df_csv.to_sql('sample', conn, if_exists='replace')
    conn.close()
    dt_now = datetime.datetime.now()
    print(dt_now)

  except Exception as e:
    print(str(e))

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1