当前位置: 首页>>代码示例>>Python>>正文


Python ExportBuilder.to_zipped_csv方法代码示例

本文整理汇总了Python中onadata.libs.utils.export_tools.ExportBuilder.to_zipped_csv方法的典型用法代码示例。如果您正苦于以下问题:Python ExportBuilder.to_zipped_csv方法的具体用法?Python ExportBuilder.to_zipped_csv怎么用?Python ExportBuilder.to_zipped_csv使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在onadata.libs.utils.export_tools.ExportBuilder的用法示例。


在下文中一共展示了ExportBuilder.to_zipped_csv方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_zipped_csv_export_works_with_unicode

# 需要导入模块: from onadata.libs.utils.export_tools import ExportBuilder [as 别名]
# 或者: from onadata.libs.utils.export_tools.ExportBuilder import to_zipped_csv [as 别名]
 def test_zipped_csv_export_works_with_unicode(self):
     """
     cvs writer doesnt handle unicode we we have to encode to ascii
     """
     survey = create_survey_from_xls(_logger_fixture_path(
         'childrens_survey_unicode.xls'))
     export_builder = ExportBuilder()
     export_builder.set_survey(survey)
     temp_zip_file = NamedTemporaryFile(suffix='.zip')
     export_builder.to_zipped_csv(temp_zip_file.name, self.data_utf8)
     temp_zip_file.seek(0)
     temp_dir = tempfile.mkdtemp()
     zip_file = zipfile.ZipFile(temp_zip_file.name, "r")
     zip_file.extractall(temp_dir)
     zip_file.close()
     temp_zip_file.close()
     # check that the children's file (which has the unicode header) exists
     self.assertTrue(
         os.path.exists(
             os.path.join(temp_dir, "children.info.csv")))
     # check file's contents
     with open(os.path.join(temp_dir, "children.info.csv")) as csv_file:
         reader = csv.reader(csv_file)
         expected_headers = ['children.info/name.first',
                             'children.info/age',
                             'children.info/fav_colors',
                             u'children.info/fav_colors/red\u2019s',
                             u'children.info/fav_colors/blue\u2019s',
                             u'children.info/fav_colors/pink\u2019s',
                             'children.info/ice_creams',
                             'children.info/ice_creams/vanilla',
                             'children.info/ice_creams/strawberry',
                             'children.info/ice_creams/chocolate', '_id',
                             '_uuid', '_submission_time', '_index',
                             '_parent_table_name', '_parent_index',
                             u'_tags', '_notes', '_version',
                             '_duration', '_submitted_by']
         rows = [row for row in reader]
         actual_headers = [h.decode('utf-8') for h in rows[0]]
         self.assertEqual(sorted(actual_headers), sorted(expected_headers))
         data = dict(zip(rows[0], rows[1]))
         self.assertEqual(
             data[u'children.info/fav_colors/red\u2019s'.encode('utf-8')],
             'True')
         self.assertEqual(
             data[u'children.info/fav_colors/blue\u2019s'.encode('utf-8')],
             'True')
         self.assertEqual(
             data[u'children.info/fav_colors/pink\u2019s'.encode('utf-8')],
             'False')
         # check that red and blue are set to true
     shutil.rmtree(temp_dir)
开发者ID:MichaelRoethlin,项目名称:onadata,代码行数:54,代码来源:test_export_builder.py

示例2: test_zipped_csv_export_works

# 需要导入模块: from onadata.libs.utils.export_tools import ExportBuilder [as 别名]
# 或者: from onadata.libs.utils.export_tools.ExportBuilder import to_zipped_csv [as 别名]
    def test_zipped_csv_export_works(self):
        survey = self._create_childrens_survey()
        export_builder = ExportBuilder()
        export_builder.set_survey(survey)
        temp_zip_file = NamedTemporaryFile(suffix='.zip')
        export_builder.to_zipped_csv(temp_zip_file.name, self.data)
        temp_zip_file.seek(0)
        temp_dir = tempfile.mkdtemp()
        zip_file = zipfile.ZipFile(temp_zip_file.name, "r")
        zip_file.extractall(temp_dir)
        zip_file.close()
        temp_zip_file.close()

        # generate data to compare with
        index = 1
        indices = {}
        survey_name = survey.name
        outputs = []
        for d in self.data:
            outputs.append(
                dict_to_joined_export(d, index, indices, survey_name))
            index += 1

        # check that each file exists
        self.assertTrue(
            os.path.exists(
                os.path.join(temp_dir, "{0}.csv".format(survey.name))))
        with open(
                os.path.join(
                    temp_dir, "{0}.csv".format(survey.name))) as csv_file:
            reader = csv.reader(csv_file)
            rows = [r for r in reader]

            # open comparison file
            with open(_logger_fixture_path(
                    'csvs', 'childrens_survey.csv')) as fixture_csv:
                fixture_reader = csv.reader(fixture_csv)
                expected_rows = [r for r in fixture_reader]
                self.assertEqual(rows, expected_rows)

        self.assertTrue(
            os.path.exists(
                os.path.join(temp_dir, "children.csv")))
        with open(os.path.join(temp_dir, "children.csv")) as csv_file:
            reader = csv.reader(csv_file)
            rows = [r for r in reader]

            # open comparison file
            with open(_logger_fixture_path(
                    'csvs', 'children.csv')) as fixture_csv:
                fixture_reader = csv.reader(fixture_csv)
                expected_rows = [r for r in fixture_reader]
                self.assertEqual(rows, expected_rows)

        self.assertTrue(
            os.path.exists(
                os.path.join(temp_dir, "children_cartoons.csv")))
        with open(os.path.join(temp_dir, "children_cartoons.csv")) as csv_file:
            reader = csv.reader(csv_file)
            rows = [r for r in reader]

            # open comparison file
            with open(_logger_fixture_path(
                    'csvs', 'children_cartoons.csv')) as fixture_csv:
                fixture_reader = csv.reader(fixture_csv)
                expected_rows = [r for r in fixture_reader]
                self.assertEqual(rows, expected_rows)

        self.assertTrue(
            os.path.exists(
                os.path.join(temp_dir, "children_cartoons_characters.csv")))
        with open(os.path.join(
                temp_dir, "children_cartoons_characters.csv")) as csv_file:
            reader = csv.reader(csv_file)
            rows = [r for r in reader]

            # open comparison file
            with open(_logger_fixture_path(
                    'csvs',
                    'children_cartoons_characters.csv')) as fixture_csv:
                fixture_reader = csv.reader(fixture_csv)
                expected_rows = [r for r in fixture_reader]
                self.assertEqual(rows, expected_rows)

        shutil.rmtree(temp_dir)
开发者ID:199212151746,项目名称:onadata,代码行数:87,代码来源:test_export_builder.py


注:本文中的onadata.libs.utils.export_tools.ExportBuilder.to_zipped_csv方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。