zoukankan      html  css  js  c++  java
  • name 'reload' is not defined解决方法

    今天在学习scrapy的时候,在网上找了一段代码,运行出了一点问题。

    命令行报错:

    name 'reload' is not defined

    原因是,python版本的问题

    原代码如下:

    import time
    import sys
    reload(sys)
    sys.setdefaultencoding('utf8')
     
     
    class Meiju100Pipeline(object):
        def process_item(self, item, spider):
            today = time.strftime('%Y%m%d',time.localtime())
            fileName = today + 'movie.txt'
            with open(fileName,'a') as fp:
                fp.write(item['storyName'][0].encode("utf8") + '	' + item['storyState'][0].encode("utf8") + '	' + item['tvStation'][0] + '	' + item['updateTime'][0] + '
    ')
            return item

    此代码为python2.7版本,

    reload(sys)              #重新加载sys模块
    sys.setdefaultencoding('utf8')   #设置默认编码格式为utf-8

    在3.x版本中,应改成如下:

    import time
    import sys
    import importlib
    importlib.reload(sys)
    #sys.setdefaultencoding('utf8')
     
    class Meiju100Pipeline(object):
        def process_item(self, item, spider):
            today = time.strftime('%Y%m%d',time.localtime())
            fileName = today + 'movie.txt'
            with open(fileName,'a') as fp:
                fp.write(item['storyName'][0].encode("utf8") + '	' + item['storyState'][0].encode("utf8") + '	' + item['tvStation'][0] + '	' + item['updateTime'][0] + '
    ')
            return item
    设置编码格式的代码可以注释掉,因为3.x版本中默认就是utf-8编码。
  • 相关阅读:
    kernel-devel-3.10.0-957.el7.x86_64.rpm kernel-headers-3.10.0-957.el7.x86_64.rpm
    解决MySQL安装:找不到msvcr120.dll和msvcp120.dll
    Node.js安装
    大数据电商数据仓库
    spark
    redis 脑裂等极端情况分析
    Redis解决并发超卖问题
    解决OutOfMemoryError: unable to create new native thread问题
    好用的java工具
    java初始化和实例化
  • 原文地址:https://www.cnblogs.com/zrmw/p/10550477.html
Copyright © 2011-2022 走看看