遇到问题汇总—Python 篇

    技术2023-05-24  91

    Python 篇目录

    问题一,UTF-8设置问题问题出现解决办法 问题二,init设置问题问题出现解决办法 问题三,第三方库requests提示警告解决办法 问题四,jupyter notebook运行提示CreateFile() Error: 5解决办法 问题五,import TensorFlow 运行提示错误解决办法 问题六,npz文件无法获取解决解决办法 No module named 'urllib2'字符串

    问题一,UTF-8设置问题

    问题出现

    使用Geany编辑器运行

    # encoding=utf-8 import jieba seg_list = jieba.cut("我来到北京清华大学", cut_all=True) print("Full Mode: " + "/ ".join(seg_list)) # 全模式

    执行提示,SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0xce in position 0: invalid continuation byte

    解决办法

    一般解决办法是在代码中加# encoding=utf-8,同时需要对Geany编辑器设置,文档—>设置文件编码—>Unicode—>Unicode(UTF-8)

    问题二,init设置问题

    问题出现

    创建RandomWalk()类,函数fill_walk()来调用,使用Geany编辑器运行

    # encoding=utf-8 from random import choice class RandomWalk(): """一个生成随机漫步数据的类""" def _init_(self, num_points = 500): """初始化随机漫步的属性""" self.num_points = num_points # 所有随机漫步都始于(0,0) self.x_values = [0] self.y_values = [0] .... # encoding=utf-8 import matplotlib.pyplot as plt from random_walk import RandomWalk #创建一个RandomWalk实例,并将其包含的点位都绘制出来 rw = RandomWalk() rw.fill_walk() plt.scatter(rw.x_values,rw.y_values, s=15) plt.show()

    执行提示,while len(self.x_values)< self.num_points: AttributeError: 'RandomWalk' object has no attribute 'x_values'

    解决办法

    Python创建类的__init__一定要两边是双下划线!!!

    问题三,第三方库requests提示警告

    # encoding=utf-8 import requests import pygal from pygal.style import LightColorizedStyle as LCS,LightenStyle as LS # 执行API调用并存储响应 url = 'https://api.github.com/search/repositories?q=language:python&sort=stars' r = requests.get(url) print("Status code:",r.status_code) ....

    执行提示,ConnectionResetError: [WinError 10054] 远程主机强迫关闭了一个现有的连接

    解决办法

    怀疑是requests过于频繁,被api.github认为是攻击行为。可以修改如下:

    # encoding=utf-8 import requests import pygal import socket import time from pygal.style import LightColorizedStyle as LCS,LightenStyle as LS # 执行API调用并存储响应 url = 'https://api.github.com/search/repositories?q=language:python&sort=stars' time.sleep(2) requests.packages.urllib3.disable_warnings() r = requests.get(url,verify=False) print("Status code:",r.status_code) ....

    问题四,jupyter notebook运行提示CreateFile() Error: 5

    解决办法

    CreateFile() Error: 5是win10系统经常会出现的,就是权限问题。 所以这里我们去注册表把权限给关闭掉。 打开注册表: regedit HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\System 修改这个路径下的键值:EnableLUA 从1 设置为0

    问题五,import TensorFlow 运行提示错误

    ImportError: Could not find the DLL(s) 'msvcp140_1.dll'. TensorFlow requires that these DLLs be installed in a directory that is named in your %PATH% environment variable.

    解决办法

    https://support.microsoft.com/zh-cn/help/2977003/the-latest-supported-visual-c-downloads 重新下载64位的vc_redist.x64.exe 文件,安装后重启就好了

    问题六,npz文件无法获取解决

    D:\Python\Keras\Lib\site-packages\tensorflow\python\keras\datasets\mnist.py 里面的mnist.npz文件默认是从https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz下载的,可能大陆网无法登录获取,可以自己定义文件路径。Downloading data from mnist.npz文件下载地址如下,我们可以将该文件替换到mnist.py中。 https://s3.amazonaws.com/img-datasets/mnist.npz

    解决办法
    origin_folder = 'https://storage.googleapis.com/tensorflow/tf-keras-datasets/' path = get_file( path, origin=origin_folder + 'mnist.npz', file_hash= '731c5ac602752760c8e48fbffcf8c3b850d9dc2a2aedcf2cc48468fc17b673d1') with np.load(path, allow_pickle=True) as f: x_train, y_train = f['x_train'], f['y_train'] x_test, y_test = f['x_test'], f['y_test']

    改成

    path = 'D:/Python/machinemnist.npz' f = np.load(path) x_train, y_train = f['x_train'], f['y_train'] x_test, y_test = f['x_test'], f['y_test'] f.close()

    No module named ‘urllib2’

    提示ModuleNotFoundError: No module named 'urllib2' 在python3里面,用urllib.request代替urllib2

    字符串

    关于解决’\u’开头的字符串转中文的方法。

    print(content)

    改成

    print(content.decode('unicode_escape'))
    Processed: 0.012, SQL: 9