当前位置:   article > 正文

python reload is not defined_name 'reload' is not defined解决方法

python reload未定义

今天在学习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") + '\t' + item['storyState'][0].encode("utf8") + '\t' + item['tvStation'][0] + '\t' + item['updateTime'][0] + '\n')

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") + '\t' + item['storyState'][0].encode("utf8") + '\t' + item['tvStation'][0] + '\t' + item['updateTime'][0] + '\n')

return item

设置编码格式的代码可以注释掉,因为3.x版本中默认就是utf-8编码。

python __file__ is not defined 解决方法

python __file__ is not defined 解决方法 __file__ 是在python module 被导入的时候生成的一个变量,所以在 __file__ 不能被使用,但是又想获取 ...

Electron "jQuery/$ is not defined" 解决方法

参考问题:https://stackoverflow.com/questions/32621988/electron-jquery-is-not-defined

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/904047
推荐阅读
相关标签
  

闽ICP备14008679号