- with gzip.open(str(data_file), 'rt') as fd, CopyBuffer() as buf:
- for row in csv.DictReader(fd, delimiter='\t', quotechar='|'):
- wd_id = int(row['wikidata_id'][1:])
- buf.add(row['language'], row['title'], row['importance'],
- None if wd_id in wd_done else row['wikidata_id'])
- wd_done.add(wd_id)
-
- if buf.size() > 10000000:
- with conn.cursor() as cur:
- buf.copy_out(cur, 'wikimedia_importance',
- columns=['language', 'title', 'importance',
- 'wikidata'])
+ copy_cmd = """COPY wikimedia_importance(language, title, importance, wikidata)
+ FROM STDIN"""
+ with gzip.open(str(data_file), 'rt') as fd, cur.copy(copy_cmd) as copy:
+ for row in csv.DictReader(fd, delimiter='\t', quotechar='|'):
+ wd_id = int(row['wikidata_id'][1:])
+ copy.write_row((row['language'],
+ row['title'],
+ row['importance'],
+ None if wd_id in wd_done else row['wikidata_id']))
+ wd_done.add(wd_id)