• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python smartcsv.reader函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中smartcsv.reader函数的典型用法代码示例。如果您正苦于以下问题:Python reader函数的具体用法?Python reader怎么用?Python reader使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



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

示例1: test_required_column_with_data_is_transformed

    def test_required_column_with_data_is_transformed(self):
        """Should apply the transformation to the value"""
        iphone_data = {"title": "iPhone 5C", "price": "799"}
        ipad_data = {"title": "iPad mini", "price": "699"}
        csv_data = """
title,price
{iphone_row}
{ipad_row}
""".format(
            iphone_row="{title},{price}".format(**iphone_data), ipad_row="{title},{price}".format(**ipad_data)
        )
        reader = smartcsv.reader(
            StringIO(csv_data),
            columns=[
                {"name": "title", "required": True},
                {"name": "price", "required": True, "transform": lambda x: Decimal(x)},
            ],
        )

        iphone = next(reader)
        ipad = next(reader)

        self.assertRaises(StopIteration, lambda: list(next(reader)))

        self.assertTrue(isinstance(iphone, dict) and isinstance(ipad, dict))

        self.assertModelsEquals(iphone, {"title": "iPhone 5C", "price": Decimal("799")})
        self.assertModelsEquals(ipad, {"title": "iPad mini", "price": Decimal("699")})
开发者ID:jangeador,项目名称:smartcsv,代码行数:28,代码来源:test_column_value_transformation.py


示例2: test_error_exception_is_reported_without_fail_fast

    def test_error_exception_is_reported_without_fail_fast(self):
        """reader.errors should contain the exception that happenend with the value transformation"""
        iphone_data = {"title": "iPhone 5C", "price": "INVALID"}
        ipad_data = {"title": "iPad mini", "price": "699"}
        iphone_row = "{title},{price}".format(**iphone_data)

        csv_data = """
title,price
{iphone_row}
{ipad_row}
""".format(
            iphone_row=iphone_row, ipad_row="{title},{price}".format(**ipad_data)
        )
        reader = smartcsv.reader(
            StringIO(csv_data),
            columns=[
                {"name": "title", "required": True},
                {"name": "price", "required": True, "transform": lambda x: Decimal(x)},
            ],
            fail_fast=False,
        )

        ipad = next(reader)

        self.assertRaises(StopIteration, lambda: list(next(reader)))
        self.assertTrue(isinstance(ipad, dict))
        self.assertModelsEquals(ipad, {"title": "iPad mini", "price": Decimal("699")})

        self.assertTrue(reader.errors is not None)
        self.assertTrue("rows" in reader.errors)
        self.assertEqual(len(reader.errors["rows"]), 1)  # 1 row failing

        self.assertRowError(reader.errors, iphone_row, 0, "transform")
开发者ID:jangeador,项目名称:smartcsv,代码行数:33,代码来源:test_column_value_transformation.py


示例3: test_not_required_value_is_provided

    def test_not_required_value_is_provided(self):
        """Shouldn't fail and should have an the correct value"""
        iphone_data = {
            'title': 'iPhone 5C',
            'price': '799'
        }
        ipad_data = {
            'title': 'iPad mini',
            'price': '699'
        }
        csv_data = """
title,price
{iphone_row}
{ipad_row}
        """.format(
            iphone_row="{title},{price}".format(**iphone_data),
            ipad_row="{title},{price}".format(**ipad_data)
        )
        reader = smartcsv.reader(StringIO(csv_data), columns=[
            {'name': 'title', 'required': False},
            {'name': 'price', 'required': False},
        ])

        iphone = next(reader)
        ipad = next(reader)

        self.assertRaises(StopIteration, lambda: list(next(reader)))

        self.assertTrue(isinstance(iphone, dict) and isinstance(ipad, dict))

        self.assertModelsEquals(iphone, iphone_data)
        self.assertModelsEquals(ipad, ipad_data)
开发者ID:ayokasystems,项目名称:smartcsv,代码行数:32,代码来源:test_required_column.py


示例4: test_required_value_is_missing_and_dont_fail_fast

    def test_required_value_is_missing_and_dont_fail_fast(self):
        """Shouldn't fail fast, instead report errors on reader.errors"""
        iphone_data = {
            'title': 'iPhone 5C'
        }
        ipad_data = {
            'title': 'iPad mini',
            'price': '699'
        }
        iphone_row = "{title},".format(**iphone_data)
        csv_data = """
title,price
{iphone_row}
{ipad_row}
        """.format(
            iphone_row=iphone_row,
            ipad_row="{title},{price}".format(**ipad_data)
        )
        reader = smartcsv.reader(StringIO(csv_data), columns=[
            {'name': 'title', 'required': True},
            {'name': 'price', 'required': True},
        ], fail_fast=False)

        ipad = next(reader)

        self.assertRaises(StopIteration, lambda: list(next(reader)))
        self.assertTrue(isinstance(ipad, dict))
        self.assertModelsEquals(ipad, ipad_data)

        self.assertTrue(reader.errors is not None)
        self.assertTrue('rows' in reader.errors)
        self.assertEqual(len(reader.errors['rows']), 1)  # 1 row failing

        self.assertRowError(
            reader.errors, iphone_row, 0, 'price')
开发者ID:ayokasystems,项目名称:smartcsv,代码行数:35,代码来源:test_required_column.py


示例5: test_default_value_is_not_transformed

    def test_default_value_is_not_transformed(self):
        """Shouldn't apply no transformation if the value is missing and the default value is being used"""
        iphone_data = {"title": "iPhone 5C", "price": ""}
        ipad_data = {"title": "iPad mini", "price": ""}
        csv_data = """
title,price
{iphone_row}
{ipad_row}
""".format(
            iphone_row="{title},{price}".format(**iphone_data), ipad_row="{title},{price}".format(**ipad_data)
        )
        mocked_validator = mock.MagicMock(return_value=True)
        reader = smartcsv.reader(
            StringIO(csv_data),
            columns=[
                {"name": "title", "required": True},
                {"name": "price", "required": False, "transform": mocked_validator, "default": 899},
            ],
        )

        iphone = next(reader)
        ipad = next(reader)

        self.assertRaises(StopIteration, lambda: list(next(reader)))

        self.assertTrue(isinstance(iphone, dict) and isinstance(ipad, dict))

        self.assertModelsEquals(iphone, {"title": "iPhone 5C", "price": 899})
        self.assertModelsEquals(ipad, {"title": "iPad mini", "price": 899})

        self.assertEqual(mocked_validator.call_count, 0)
开发者ID:jangeador,项目名称:smartcsv,代码行数:31,代码来源:test_column_value_transformation.py


示例6: test_column_required_and_choice_missing_with_fail_fast

    def test_column_required_and_choice_missing_with_fail_fast(self):
        """Should fail because the field is required"""
        iphone_data = {
            'title': 'iPhone 5C'
        }
        ipad_data = {
            'title': 'iPad mini',
            'currency': 'ARS'
        }
        csv_data = """
title,currency
{iphone_row}
{ipad_row}""".format(
            iphone_row="{title},".format(**iphone_data),
            ipad_row="{title},{currency}".format(**ipad_data)
        )
        reader = smartcsv.reader(StringIO(csv_data), columns=[
            {'name': 'title', 'required': True},
            {
                'name': 'currency',
                'required': True,
                'choices': CURRENCY_CHOICES
            }
        ])

        try:
            next(reader)
        except InvalidCSVException as e:
            self.assertTrue(e.errors is not None)
            self.assertTrue('currency' in e.errors)
        else:
            assert False, "Shouldn't reach this state"
开发者ID:ayokasystems,项目名称:smartcsv,代码行数:32,代码来源:test_column_choices.py


示例7: test_invalid_value_is_passed_and_exception_is_raised

    def test_invalid_value_is_passed_and_exception_is_raised(self):
        """Should not validate and raise a exception (fail_fast=True)"""
        iphone_data = {
            'title': 'iPhone 5C',
            'price': 'INVALID'
        }
        ipad_data = {
            'title': 'iPad mini',
            'price': '699'
        }
        csv_data = """
title,price
{iphone_row}
{ipad_row}""".format(
            iphone_row="{title},{price}".format(**iphone_data),
            ipad_row="{title},{price}".format(**ipad_data)
        )
        reader = smartcsv.reader(StringIO(csv_data), columns=[
            {'name': 'title', 'required': True},
            {
                'name': 'price',
                'required': True,
                'validator': is_number
            },
        ])

        try:
            next(reader)
        except InvalidCSVException as e:
            self.assertTrue(e.errors is not None)
            self.assertTrue('price' in e.errors)
开发者ID:ayokasystems,项目名称:smartcsv,代码行数:31,代码来源:test_column_validation.py


示例8: test_column_doesnt_have_a_name

 def test_column_doesnt_have_a_name(self):
     """Should fail if a column doesn't have a name"""
     columns = [{"required": True}, {"name": "price", "required": True}]
     self.assertRaises(
         InvalidCSVColumnDefinition,
         lambda: smartcsv.reader(StringIO(CSV_DATA), columns=columns, header_included=False),
     )
开发者ID:jangeador,项目名称:smartcsv,代码行数:7,代码来源:test_model_definitions.py


示例9: test_required_column_empty_fails_even_with_default

    def test_required_column_empty_fails_even_with_default(self):
        """Should fail if the column is required and the value is empty."""
        iphone_data = {
            'title': 'iPhone 5C',
            'price': ''
        }
        ipad_data = {
            'title': 'iPad mini',
            'price': '799'
        }
        csv_data = """
title,price
{iphone_row}
{ipad_row}
""".format(
            iphone_row="{title},{price}".format(**iphone_data),
            ipad_row="{title},{price}".format(**ipad_data)
        )
        reader = smartcsv.reader(StringIO(csv_data), columns=[
            {'name': 'title', 'required': True},
            {'name': 'price', 'required': True, 'default': 999},
        ])

        try:
            next(reader)
        except InvalidCSVException as e:
            self.assertTrue(e.errors is not None)
            self.assertTrue('price' in e.errors)
开发者ID:ayokasystems,项目名称:smartcsv,代码行数:28,代码来源:test_column_default.py


示例10: test_not_required_column_empty_returns_default_value

    def test_not_required_column_empty_returns_default_value(self):
        """Should return the default value if no other is provided"""
        iphone_data = {
            'title': 'iPhone 5C',
            'price': ''
        }
        ipad_data = {
            'title': 'iPad mini',
            'price': '799'
        }
        csv_data = """
title,price
{iphone_row}
{ipad_row}
""".format(
            iphone_row="{title},{price}".format(**iphone_data),
            ipad_row="{title},{price}".format(**ipad_data)
        )
        reader = smartcsv.reader(StringIO(csv_data), columns=[
            {'name': 'title', 'required': True},
            {'name': 'price', 'required': False, 'default': 999},
        ])

        iphone = next(reader)
        ipad = next(reader)

        self.assertRaises(StopIteration, lambda: list(next(reader)))

        self.assertTrue(isinstance(iphone, dict) and isinstance(ipad, dict))

        self.assertModelsEquals(iphone, {
            'title': 'iPhone 5C',
            'price': 999
        })
        self.assertModelsEquals(ipad, ipad_data)
开发者ID:ayokasystems,项目名称:smartcsv,代码行数:35,代码来源:test_column_default.py


示例11: test_valid_and_value_transformed_with_only_required_data

    def test_valid_and_value_transformed_with_only_required_data(self):
        """Should transform all values with only required data present"""
        csv_data = """
title,currency,price,in_stock
iPhone 5c blue,USD,799,
iPad mini,USD,699,
"""
        reader = smartcsv.reader(
            StringIO(csv_data), columns=COLUMNS_WITH_VALUE_TRANSFORMATIONS)
        iphone = next(reader)
        ipad = next(reader)

        self.assertRaises(StopIteration, lambda: list(next(reader)))

        self.assertTrue(
            isinstance(iphone, dict) and isinstance(ipad, dict))

        self.assertModelsEquals(iphone, {
            'title': 'iPhone 5c blue',
            'currency': 'USD',
            'price': Decimal('799'),
            'in_stock': ''
        })
        self.assertModelsEquals(ipad, {
            'title': 'iPad mini',
            'currency': 'USD',
            'price': Decimal('699'),
            'in_stock': ''
        })
开发者ID:ayokasystems,项目名称:smartcsv,代码行数:29,代码来源:test_value_transformations.py


示例12: test_valid_data_and_skip_lines_with_header

    def test_valid_data_and_skip_lines_with_header(self):
        """Should skip the first N lines and parse data ok with header"""
        csv_data = """
Generated by Autobot 2000 - V0.1.2
----------
This next is intentionally left blank

-- Beginning of content
title,category,subcategory,currency,price,url,image_url
{iphone_data}
{ipad_data}
        """.format(
            iphone_data=VALID_TEMPLATE_STR.format(**IPHONE_DATA),
            ipad_data=VALID_TEMPLATE_STR.format(**IPAD_DATA),
        )
        reader = smartcsv.reader(
            StringIO(csv_data), columns=COLUMNS_1, skip_lines=6)

        iphone = next(reader)
        ipad = next(reader)

        self.assertRaises(StopIteration, lambda: list(next(reader)))

        self.assertTrue(
            isinstance(iphone, dict) and isinstance(ipad, dict))

        self.assertModelsEquals(iphone, IPHONE_DATA)
        self.assertModelsEquals(ipad, IPAD_DATA)
开发者ID:ilmesi,项目名称:smartcsv,代码行数:28,代码来源:test_csv_skip_lines.py


示例13: test_default_value_is_empty

 def test_default_value_is_empty(self):
     """Should fail a default value is empty"""
     columns = [{"name": "title", "required": True}, {"name": "price", "required": True, "default": ""}]
     self.assertRaises(
         InvalidCSVColumnDefinition,
         lambda: smartcsv.reader(StringIO(CSV_DATA), columns=columns, header_included=False),
     )
开发者ID:jangeador,项目名称:smartcsv,代码行数:7,代码来源:test_model_definitions.py


示例14: handle

    def handle(self, *args, **options):
        if len(args) != 1:
            raise CommandError('Usage: importproducts ' + self.args)

        filepath = args[0]

        print 'Importing persons from {0}...'.format(filepath)

        stats = {'imported': 0, 'skipped': 0}
        with open(filepath) as f:
            reader = smartcsv.reader(f, columns=COLUMNS, fail_fast=False)

            for person in reader:
                try:
                    Profile.objects.create(
                        email=person['email'],
                        lg_full_name=person['name'],
                        lg_timezone=person['timezone']
                    )
                except IntegrityError:
                    stats['skipped'] += 1
                else:
                    stats['imported'] += 1
        print ('Finished with stats: '
               'imported {imported}, skipped {skipped}').format(**stats)
开发者ID:adrianmoisey,项目名称:rmotr-sis,代码行数:25,代码来源:importpersons.py


示例15: test_valid_choice_is_provided

    def test_valid_choice_is_provided(self):
        """Should not fail and have the value of the selected choice"""
        iphone_data = {
            'title': 'iPhone 5C',
            'currency': 'USD'
        }
        ipad_data = {
            'title': 'iPad mini',
            'currency': 'ARS'
        }
        csv_data = """
title,currency
{iphone_row}
{ipad_row}""".format(
            iphone_row="{title},{currency}".format(**iphone_data),
            ipad_row="{title},{currency}".format(**ipad_data)
        )
        reader = smartcsv.reader(StringIO(csv_data), columns=[
            {'name': 'title', 'required': True},
            {
                'name': 'currency',
                'required': True,
                'choices': CURRENCY_CHOICES
            }
        ])

        iphone = next(reader)
        ipad = next(reader)

        self.assertRaises(StopIteration, lambda: list(next(reader)))

        self.assertTrue(isinstance(iphone, dict) and isinstance(ipad, dict))

        self.assertModelsEquals(iphone, iphone_data)
        self.assertModelsEquals(ipad, ipad_data)
开发者ID:ayokasystems,项目名称:smartcsv,代码行数:35,代码来源:test_column_choices.py


示例16: test_required_value_is_missing_and_fail_fast

    def test_required_value_is_missing_and_fail_fast(self):
        """Should fail fast and report the error"""
        iphone_data = {
            'title': 'iPhone 5C'
        }
        ipad_data = {
            'title': 'iPad mini',
            'price': '699'
        }
        csv_data = """
title,price
{iphone_row}
{ipad_row}
        """.format(
            iphone_row="{title},".format(**iphone_data),
            ipad_row="{title},{price}".format(**ipad_data)
        )
        reader = smartcsv.reader(StringIO(csv_data), columns=[
            {'name': 'title', 'required': True},
            {'name': 'price', 'required': True},
        ])

        try:
            next(reader)
        except InvalidCSVException as e:
            self.assertTrue(e.errors is not None)
            self.assertTrue('price' in e.errors)
        else:
            assert False, "Shouldn't reach this state"
开发者ID:ayokasystems,项目名称:smartcsv,代码行数:29,代码来源:test_required_column.py


示例17: test_valid_csv_with_blank_lines

    def test_valid_csv_with_blank_lines(self):
        """Should be valid even if there are blank lines"""
        csv_data = """

title,category,subcategory,currency,price,url,image_url


{iphone_data}

{ipad_data}


        """.format(
            iphone_data=VALID_TEMPLATE_STR.format(**IPHONE_DATA),
            ipad_data=VALID_TEMPLATE_STR.format(**IPAD_DATA),
        )

        reader = smartcsv.reader(StringIO(csv_data), columns=COLUMNS_1)
        iphone = next(reader)
        ipad = next(reader)

        self.assertRaises(StopIteration, lambda: list(next(reader)))

        self.assertTrue(
            isinstance(iphone, dict) and isinstance(ipad, dict))

        self.assertModelsEquals(iphone, IPHONE_DATA)
        self.assertModelsEquals(ipad, IPAD_DATA)
开发者ID:ilmesi,项目名称:smartcsv,代码行数:28,代码来源:test_valid_csv.py


示例18: test_valid_and_white_spaces_for_values_are_not_stripped

    def test_valid_and_white_spaces_for_values_are_not_stripped(self):
        """Should strip white spaces out of values"""
        iphone_data = IPHONE_DATA.copy()
        ipad_data = IPAD_DATA.copy()

        iphone_data['category'] = "     Phones   "
        ipad_data['price'] = " 599  "

        csv_data = """
title,category,subcategory,currency,price,url,image_url
    {iphone_data}
{ipad_data}
        """.format(
            iphone_data=VALID_TEMPLATE_STR.format(**iphone_data),
            ipad_data=VALID_TEMPLATE_STR.format(**ipad_data),
        )
        reader = smartcsv.reader(
            StringIO(csv_data), columns=COLUMNS_1, strip_white_spaces=False)
        iphone = next(reader)
        ipad = next(reader)

        self.assertRaises(StopIteration, lambda: list(next(reader)))

        self.assertTrue(
            isinstance(iphone, dict) and isinstance(ipad, dict))

        # First 4 spaces
        iphone_data['title'] = "    " + iphone_data['title']
        self.assertModelsEquals(iphone, iphone_data)
        self.assertModelsEquals(ipad, ipad_data)
开发者ID:ilmesi,项目名称:smartcsv,代码行数:30,代码来源:test_valid_csv.py


示例19: test_valid_csv_with_a_custom_dialect

    def test_valid_csv_with_a_custom_dialect(self):
        """Should be valid if all data is passed"""
        piped_template = VALID_TEMPLATE_STR.replace(',', '|')
        csv_data = """
title|category|subcategory|currency|price|url|image_url
{iphone_data}
{ipad_data}
            """.format(
            iphone_data=piped_template.format(**IPHONE_DATA),
            ipad_data=piped_template.format(**IPAD_DATA),
        )

        csv.register_dialect('pipes', delimiter='|')

        reader = smartcsv.reader(
            StringIO(csv_data), dialect='pipes', columns=COLUMNS_1)
        iphone = next(reader)
        ipad = next(reader)

        self.assertRaises(StopIteration, lambda: list(next(reader)))

        self.assertTrue(
            isinstance(iphone, dict) and isinstance(ipad, dict))

        self.assertModelsEquals(iphone, IPHONE_DATA)
        self.assertModelsEquals(ipad, IPAD_DATA)
开发者ID:ilmesi,项目名称:smartcsv,代码行数:26,代码来源:test_valid_csv.py


示例20: test_valid_csv_with_some_fields_not_required_empty

    def test_valid_csv_with_some_fields_not_required_empty(self):
        """Should be valid if some not required fields are filled and others don't"""

        iphone_data = IPHONE_DATA.copy()
        ipad_data = IPAD_DATA.copy()

        iphone_data['subcategory'] = ""
        iphone_data['image_url'] = ""

        ipad_data['image_url'] = ""

        csv_data = """
        title,category,subcategory,currency,price,url,image_url
{iphone_data}
{ipad_data}
        """.format(
            iphone_data=VALID_TEMPLATE_STR.format(**iphone_data),
            ipad_data=VALID_TEMPLATE_STR.format(**ipad_data),
        )
        reader = smartcsv.reader(StringIO(csv_data), columns=COLUMNS_1)
        iphone = next(reader)
        ipad = next(reader)

        self.assertRaises(StopIteration, lambda: list(next(reader)))

        self.assertTrue(isinstance(iphone, dict) and isinstance(ipad, dict))

        self.assertEqual(iphone['title'], 'iPhone 5c blue')
        self.assertEqual(ipad['title'], 'iPad mini')
        self.assertEqual(iphone['subcategory'], '')
        self.assertEqual(ipad['subcategory'], 'Apple')
开发者ID:ilmesi,项目名称:smartcsv,代码行数:31,代码来源:test_valid_csv.py



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python twitter_user.TwitterUser类代码示例发布时间:2022-05-27
下一篇:
Python util.toHexString函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap