Posts

Showing posts from July, 2017

[Python][Memo] File modes

All file modes in Python r Opens a file for reading only. The file pointer is placed at the beginning of the file. This is the default mode. rb Opens a file for reading only in binary format. The file pointer is placed at the beginning of the file. This is the default mode. r+ Opens a file for both reading and writing. The file pointer will be at the beginning of the file. rb+ Opens a file for both reading and writing in binary format. The file pointer will be at the beginning of the file. w Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing. wb Opens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing. w+ Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing. wb+ Opens a file for both writing and reading...

[PYTHON] [MEMO] Read File, Converting, Insert to DB

from contextlib import closing import sqlite3 dbname = 'moji.db' def method(): with open('text.txt', 'r') as f1, open('sss.txt', 'r') as f2, closing(sqlite3.connect(dbname)) as conn: c = conn.cursor() create_table = 'create table moji(unicode int, sjis int, jis int, width int, height int, contents text)' c.execute(create_table) sql = 'insert into moji values(?,?,?,?,?,?)' while True: f1line = f1.readline() f2line = f2.readline() a = f1line.split(',') b = f2line.split(',') a0 = a[0] a1 = a[1] a2 = a[2] b0 = b[0] b1 = b[1] b2 = b[2] width = 16 height = 16 contents = f1line[-160:] jis = int(b1,16) unicode = int(b2,16) sjis = int(b0,16) moji = (uni...