def test_writer(self): writer = csvkit.CSVKitDictWriter(self.output, ['a', 'b', 'c']) writer.writeheader() writer.writerow({u'a': u'1', u'b': u'2', u'c': u'☃'}) result = self.output.getvalue() self.assertEqual(result, 'a,b,c\n1,2,☃\n')
def write_files(self): """From memory informations (self.db) creates all required csv files. """ regions_columns = [u'ID', u'Denominazione regione'] topic_columns = [u'ID', u'Denominazione tema sintetico'] index_columns = [u'ID', u'Titolo', u'Sottotitolo'] topic_index_columns = [u'Regione'] + [unicode(year) for year in self.db.years] self.logger.info(u'Writing {0} regions: {1}'.format(len(self.db.regions), self.db.regions.values())) with open(REGIONS_CSV, 'wb') as csv_file: writer = csvkit.CSVKitDictWriter(csv_file, regions_columns) writer.writeheader() for location_id in sorted(self.db.regions): writer.writer.writerow([unicode(location_id), self.db.regions.get(location_id)]) self.logger.info(u'Writing {0} topics: {1}'.format(len(self.db.topics), self.db.topics.values())) with open(TOPICS_CSV, 'wb') as csv_file: writer = csvkit.CSVKitDictWriter(csv_file, topic_columns) writer.writeheader() for topic_id in sorted(self.db.topics): writer.writer.writerow([unicode(topic_id), self.db.topics.get(topic_id)]) for topic_id in sorted(self.db.indexes_by_topic): self.logger.info(u'[{0}] {1}'.format(topic_id, self.db.topics.get(topic_id))) with open(static_topic(topic_id), 'wb') as csv_file: writer = csvkit.CSVKitDictWriter(csv_file, index_columns) writer.writeheader() for index_id in sorted(self.db.indexes_by_topic.get(topic_id)): index = self.db.indexes.get(index_id) self.logger.info(u' |-[{0}-{1}] {2}'.format(topic_id, index_id, index.get('title'))) writer.writer.writerow([unicode(index_id), index.get('title'), index.get('subtitle')]) with open(static_topic_index(topic_id, index_id), 'wb') as csv_index_file: index_writer = csvkit.CSVKitDictWriter(csv_index_file, topic_index_columns) index_writer.writeheader() for location_id in sorted(self.db.values.get(index_id)): values = [self.db.regions.get(location_id)] + [self.db.values.get(index_id).get(location_id).get(year, '') for year in self.db.years] index_writer.writer.writerow(values)
f_write = open("wp_munic2011_posts.csv", "w") reader = csvkit.CSVKitDictReader(f_read) posts = [] for line in reader: f_post = line f_post["id"] = '' f_post["post_title"] = f_post["wpcf-a570"] + " - " + f_post["wpcf-a569"] f_post["post_type"] = "municipio" f_post["post_status"] = "publish" f_post["comment_status"] = "open" f_post["post_author"] = f_post["ibge"] f_post["lat"] = f_post["lat"].replace(",",".") f_post["lng"] = f_post["lng"].replace(",",".") posts.append(f_post) writer = csvkit.CSVKitDictWriter(f_write, posts[0].keys()) writer.writeheader() writer.writerows(posts) f_write.close() # Prepara lista de usuarios f_write = open("wp_munic2011_users.csv", "w") authors = [] for line in posts: f_author = {} f_author["user_login"] = line["ibge"] f_author["user_pass"] = line["ibge"] f_author["user_email"] = line["ibge"] + "@deolhonosplanos.org.br" f_author["first_name"] = line["wpcf-a570"] f_author["last_name"] = line["wpcf-a569"]