Created
September 7, 2018 16:33
-
-
Save neoneo40/2a7b18b9899580f3aa74ef346650f607 to your computer and use it in GitHub Desktop.
psql ignore errors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from .models import Url | |
def get_columns(line): | |
column_str = re.search(r'COPY [-_\w\.]+ \(([^\)]+)\)', line).group(1) | |
columns = column_str.split(',') | |
return [column.strip() for column in columns] | |
def is_start(text): | |
if text.startswith('COPY '): | |
return True | |
return False | |
def is_end(text): | |
if text.startswith('\.'): | |
return True | |
return False | |
def input_data_in_db(path, db): | |
start = None | |
columns = None | |
with open(path) as f: | |
for line in f: | |
if not start: | |
start = is_start(line) | |
if start: | |
columns = get_columns(line) | |
continue | |
if is_end(line): | |
break | |
dic = {} | |
for i, column in enumerate(columns): | |
dic[column] = line.split('\t')[i].strip() | |
if dic[column] == '\n' or dic[column] == '': | |
dic[column] = None | |
print(dic) | |
try: | |
obj = db(**dic) | |
obj.save() | |
except Exception as e: | |
print(e) | |
path = 'data.dump' | |
db = Url | |
input_data_in_db(path=path, db=db) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment