Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- from mysite.models import WikiPopularity #it might be another pass to a model
- import glob, csv
- from django.db import connection
- def stringed_chunk(chunk):
- str_chunk = ["('{0}', {1})".format(row[0], row[1]) for row in chunk]
- return ", ".join(str_chunk)
- def model_upsertion(chunk):
- chunk = stringed_chunk(chunk)
- with connection.cursor() as cursor:
- try:
- cursor.execute(
- """INSERT INTO mysite_wikipopularity (wiki_id, popularity)
- VALUES {}
- ON CONFLICT (wiki_id)
- DO UPDATE SET popularity = excluded.popularity;""".format(chunk)
- )
- except:
- return
- def quantity_of_thousands_of_lines(file_name):
- with open(file_name) as f:
- quantity_of_lines = sum(1 for line in f)
- quantaty_of_thousands_of_lines = quantity_of_lines//1000
- rest_of_lines = quantity_of_lines%1000
- return quantaty_of_thousands_of_lines, rest_of_lines
- def upsertion_with_thousands_of_lines(reader, quantaty_of_thousands):
- for i in xrange(quantaty_of_thousands):
- chunk_of_rows = [reader.next() for x in xrange(1000)]
- model_upsertion(chunk_of_rows)
- def upsertion_with_rest_of_lines(reader, rest_of_lines):
- chunk_of_rows = [reader.next() for i in xrange(rest_of_lines)]
- model_upsertion(chunk_of_rows)
- def model_upsertion_with_file(csv_file_name):
- quantaty_of_thousands, rest_of_lines = quantity_of_thousands_of_lines(csv_file_name)
- with open(csv_file_name) as f:
- reader = csv.reader(f)
- upsertion_with_thousands_of_lines(reader, quantaty_of_thousands)
- upsertion_with_rest_of_lines(reader, rest_of_lines)
- def upsert():
- csv_files = glob.glob('*.csv') #you may change path to your tmp dir
- for csv_file_name in csv_files:
- model_upsertion_with_file(csv_file_name)
- print u'success'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement