Guest User

Untitled

a guest
Apr 3rd, 2021
2,387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.29 KB | None | 0 0
  1. import pathlib
  2. import sqlite3
  3. import sys
  4.  
  5.  
  6. def get_records(dump_dir: str = '.'):
  7.     for txt in pathlib.Path(dump_dir).glob('USA *.txt'):
  8.         print(txt.as_posix())
  9.         with txt.open('r', encoding='utf-8') as records:
  10.             for record in records:
  11.                 try:
  12.                     (phone_no, user_id, fname, lname, gender, city1, city2, relationship, company,
  13.                      d1, d2, d3, email, _) = record.split(':')
  14.                 except ValueError:
  15.                     pass
  16.                 else:
  17.                     yield (phone_no, user_id, fname, lname, gender, city1, city2, relationship,
  18.                             company, f'{d1}:{d2}:{d3}', email)
  19.  
  20.  
  21. if __name__ == '__main__':
  22.     _, dst = sys.argv
  23.     db = sqlite3.connect(dst)
  24.     db.execute('PRAGMA synchronous = OFF;')
  25.     db.execute('PRAGMA journal_mode = MEMORY;')
  26.     db.execute('PRAGMA secure_delete = OFF;')
  27.     db.execute('PRAGMA locking_mode = EXCLUSIVE;')
  28.     db.isolation_level = None
  29.     db.execute('''CREATE TABLE IF NOT EXISTS usa(phone_no, user_id, fname,
  30.                  lname, gender, city1, city2, relationship, company, seen,
  31.                  email);''')
  32.     db.commit()
  33.  
  34.     db.executemany('INSERT INTO usa VALUES (?,?,?,?,?,?,?,?,?,?,?);', get_records())
  35.     db.commit()
  36.  
Advertisement
Add Comment
Please, Sign In to add comment