栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何以正确的顺序导入Scrapy项目密钥?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何以正确的顺序导入Scrapy项目密钥?

我可以使它起作用的唯一方法是以下列方式使用此解决方案。

我的

items.py
档案:

from scrapy.item import Item, Fieldfrom collections import OrderedDictfrom types import FunctionTypeclass StaticOrderHelper(type):    # Requires Python3    def __prepare__(name, bases, **kwargs):        return OrderedDict()    def __new__(mcls, name, bases, namespace, **kwargs):        namespace['_field_order'] = [     k     for k, v in namespace.items()     if not k.startswith('__') and not k.endswith('__')         and not isinstance(v, (FunctionType, classmethod, staticmethod))        ]        return type.__new__(mcls, name, bases, namespace, **kwargs)class NewAdsItem(metaclass=StaticOrderHelper):    AdId        = Field()    DateR       = Field()    AdURL       = Field()

然后使用以下命令将该

_field_order
项目导入你
piplines.py
的:

...from adbot.items import NewAdsItem...class DbPipeline(object):    ikeys = NewAdsItem._field_order    ...    def createDbTable(self):        print("Creating new table: %s" % self.dbtable )        print("Keys in creatDbTable: t%s" % ",".join(self.ikeys) )        ...

现在,我可以按照正确的外观顺序创建新的数据库表,而不必担心Python以一种意想不到的方式对字典进行奇怪的排序。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/380218.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号