
Python错误集锦
文章平均质量分 51
Python错误案例解决方案
桔子code
微信搜索[桔子code],也有干货
展开
-
Python错误集锦:faker模块生成xml文件时提示:`xml` requires the `xmltodict` Python library
1、faker模块需要用到xmltodict模块,该模块 xmltodict 并不会作为faker的依赖主动安装。1、手动安装xmltodict模块。原创 2024-06-26 20:24:28 · 636 阅读 · 0 评论 -
Python错误集锦:xlwt写入单元格提示Attempt to overwrite cell: sheetname=’juzicode’ rowx=5 colx=5
1、ws.write(5,5,’xyz’) 对单元格(5,5) 先后2次写入导致报错。原创 2024-05-26 13:35:13 · 579 阅读 · 0 评论 -
Python错误集锦:xlwt写入表格时提示exception-unexpected-data-type-class-bytes
1、xlwt写入表格的数据类型不能为bytes。原创 2024-05-26 17:48:01 · 285 阅读 · 0 评论 -
Python错误集锦:sqlite3建表时提示:sqlite3.OperationalError: table table_juzicode already exists
原文链接:http://www.juzicode.com/archives/3940错误提示:sqlite3建表时提示:sqlite3.OperationalError: table table_juzicode already exists#juzicode.com/vx:桔子codeimport sqlite3db_name = 'test.db'table_name = 'table_juzicode'conn = sqlite3.connect(db_name)cursor原创 2021-04-03 08:03:07 · 3649 阅读 · 0 评论 -
Python错误集锦:sqlite3插入记录提示:IntegrityError: UNIQUE constraint failed: table_juzicode._id
原文链接:http://www.juzicode.com/archives/3986错误提示:下列代码第2次执行时,插入记录提示:IntegrityError: UNIQUE constraint failed: table_juzicode._id#juzicode.com/vx:桔子codeimport sqlite3#检查表是否存在,存在返回Truedef check_table_exsist(cursor,table_name): sql = '''SELECT tb原创 2021-04-03 08:04:47 · 8521 阅读 · 3 评论 -
Python错误集锦:multiprocessing无法启动多个子进程
在前面的系列文章中涉及的内容都是在本地进行的,接下来介绍的内容会更有趣,会进入到一个更广阔的世界。Python中内置的socket模块可以实现多台电脑间通过网口进行通信,互相收发消息,这篇文章将从最底层的socket模块开始介绍,而更上层的ftplib,telnetlib等都是基于socket封装的更高层应用的模块。提到socket通信,必然绕不开C/S结构,在C/S结构中,其中一台电脑作为server端,等待其他的client端连接、通信。比如一个Ftp服务器启动后,会等待其他的客户端连接、传输文件。原创 2021-01-28 21:56:25 · 3047 阅读 · 3 评论 -
Python错误集锦:matplotlib imshow()方法显示图像提示:TypeError: Invalid shape (100, 100, 5) for image data
原文链接:http://www.juzicode.com/archives/3502错误提示:matplotlib的imshow()方法显示图像时提示:TypeError: Invalid shape (100, 100, 5) for image data#juzicode.com; #vx:桔子codeimport pandas as pdimport numpy as npimport matplotlib.pyplot as pltarr = np.full((100,.原创 2021-01-06 19:18:40 · 16934 阅读 · 12 评论 -
Python错误集锦:pyplot.hist()绘制直方图时提示:ValueError: x must have 2 or fewer dimensions
原文链接:http://www.juzicode.com/archives/3139错误提示:pyplot.hist()绘制直方图时提示:ValueError: x must have 2 or fewer dimensions#juzicode.com,#VX:桔子codeimport numpy as npimport matplotlib.pyplot as pltplt.rc('font',family='Youyuan',size='9')plt.rc('axes',u.原创 2020-12-30 19:44:29 · 3881 阅读 · 0 评论 -
Python错误集锦:pandas绘制多个子图时报错:AttributeError: ‘list’ object has no attribute ‘get_figure’
原文链接:http://www.juzicode.com/archives/3273错误提示:利用pandas绘制多个子图时报错:AttributeError: ‘list’ object has no attribute ‘get_figure’#juzicode.com #vx:桔子codeimport pandas as pdimport numpy as npimport matplotlib.pyplot as pltplt.rc('font',family='Youy.原创 2020-12-30 19:46:09 · 5788 阅读 · 0 评论 -
Python错误集锦:在pandas中用to_excel()写文件提示:ModuleNotFoundError: No module named ‘xlwt’
原文链接:http://www.juzicode.com/archives/3127错误提示:在pandas中用to_excel()写文件提示:ModuleNotFoundError: No module named ‘xlwt’:import numpy as npimport pandas as pdarr = np.random.randint(-50,50,size=(3,10))ind = [x for x in range(3)]columns = list('ABC.原创 2020-12-28 20:06:53 · 4544 阅读 · 0 评论 -
Python错误集锦:pandas读取excel提示ImportError: Missing optional dependency ‘xlrd’.
原文链接:http://www.juzicode.com/archives/3125错误提示:用pandas read_excel()方法读取xls或xlsx文件时,提示:ImportError: Missing optional dependency ‘xlrd’. Install xlrd >= 1.0.0 for Excel support Use pip or conda to install xlrd.import numpy as npimport pandas as.原创 2020-12-28 19:47:28 · 42689 阅读 · 1 评论 -
Python错误集锦:三引号注释提示错误:IndentationError: expected an indented block
原文链接:http://www.juzicode.com/archives/2840错误提示:定义函数时,使用三引号注释提示错误:IndentationError:expected an indented block#juzicode.com/vx:桔子codedef fuc():'''定义一个函数''' print('桔子code') print('juzicode.com') return None#调用func函数fuc()..原创 2020-12-21 21:04:04 · 2288 阅读 · 0 评论 -
Python错误集锦:开启线程时提示TypeError: func() argument after * must be an iterable, not int
原文链接:http://www.juzicode.com/archives/2756错误提示:开启线程时提示TypeError: func_one_para() argument after * must be an iterable, not int#juzicode.com/vx:桔子codeimport threadingdef func(para1): print(para1)if __name__ == '__main__': para1 =11...原创 2020-12-20 12:26:30 · 4741 阅读 · 2 评论 -
Python错误集锦:with方法打开文件提示:ValueError: I/O operation on closed file.
原文链接:http://www.juzicode.com/archives/2749错误提示:使用with方法打开文件后,再次read()文件时提示:ValueError: I/O operation on closed file.#juzicode.com/vx:桔子codewith open('example-r.txt','r') as fileobj: cont = fileobj.read() print(cont)fileobj.read()juzi.原创 2020-12-20 12:25:34 · 2447 阅读 · 1 评论 -
Python错误集锦:读写文件时提示UnsupportedOperation: not readable
原文链接:http://www.juzicode.com/archives/2745错误提示:读写文件时提示UnsupportedOperation: not readable#juzicode.com/vx:桔子codefileobj=open('example-r.txt','r')cont = fileobj.read()print(cont)fileobj=open('example-w.txt','w')cont = fileobj.read()print(con.原创 2020-12-20 12:25:17 · 1925 阅读 · 1 评论 -
Python错误集锦:字符串到10进制转换时提示:ValueError: invalid literal for int() with base 10: ‘1a‘
原文链接:http://www.juzicode.com/archives/2736错误提示:字符串到10进制转换时提示:ValueError: invalid literal for int() with base 10: ‘1a’#juzicode.com/vx:桔子codea = int('135',10)print('a=',a)b = int('1a',10)print('b=',b)a= 135--------------------------------.原创 2020-12-20 12:24:56 · 741 阅读 · 1 评论 -
Python错误集锦:使用%s格式化tuple类型的字符串:TypeError: not all arguments converted during string formatting
原文链接:http://www.juzicode.com/archives/2731错误提示:使用%s格式化tuple类型的字符串时提示:TypeError: not all arguments converted during string formatting#juzicode.com/vx:桔子codec = '桔子code'd = [1,2,3,4,5]e = (1,2,3,4,5)print('c:%s'%(c))print('d:%s'%(d))print('e:%s'原创 2020-12-20 12:24:32 · 469 阅读 · 1 评论 -
Python错误集锦:格式化字符串 IndexError: Replacement index 5 out of range for positional args tuple
原文链接:http://www.juzicode.com/archives/2725错误提示:格式化字符串时提示index超出范围:IndexError: Replacement index 5 out of range for positional args tuple#juzicode.com/vx:桔子codea = 3b = 5.12345c = '桔子code'd = [1,2,3,4,5]e = (1,2,3,4,5)f = {1,2,3,4,5}out = 'a:原创 2020-12-20 12:24:03 · 28644 阅读 · 1 评论 -
Python错误集锦:set计算并集时TypeError: unsupported operand type(s) for +: ‘set’ and ‘set’
原文链接:http://www.juzicode.com/archives/2720错误提示:#juzicode.com/vx:桔子codes1={1,2,3,4}#集合s1s2={3,4,5,6}#集合s2print('s1-s2:',s1-s2)print('s1+s2:',s1+s2)s1-s2: {1, 2}---------------------------------------------------------------------------TypeErro原创 2020-12-19 11:46:21 · 4481 阅读 · 1 评论 -
Python错误集锦:list dict类型的数据作为set元素TypeError: unhashable type: ‘list‘ TypeError: unhashable type: ‘dict
原文链接:http://www.juzicode.com/archives/2715错误提示:构建set类型的数据时,使用tuple类型的数据作为元素没有问题,使用list、dict类型的数据作为元素报TypeError: unhashable type: ‘list’、 TypeError: unhashable type: ‘dict’#juzicode.com/vx:桔子codet = ('juzicode.com', '桔子code', '橙子')l = ['juzicode.co原创 2020-12-19 11:41:34 · 687 阅读 · 1 评论 -
Python错误集锦:字典使用数字类型的下标访问时提示KeyError: 0
原文链接:http://www.juzicode.com/archives/2711错误提示:list使用数字类型的下标访问没有问题,字典使用数字类型的下标访问时提示KeyError: 0#juzicode.com/vx:桔子codel = ['juzicode.com', '桔子code', '橙子']print(l[0])d = {'A':'juzicode.com','B':'桔子code','C':'橙子'}print(d[0])juzicode.com--------原创 2020-12-19 11:32:21 · 2789 阅读 · 2 评论 -
Python错误集锦:list类型的数据作为字典的key提示TypeError: unhashable type: ‘list’
原文链接:http://www.juzicode.com/archives/2706错误提示:用tuple类型的数据作为字典的key不报错,用list类型的数据作为字典的key时,提示TypeError: unhashable type: ‘list’#juzicode.com/vx:桔子codetuple1 = (1,2,3)tuple2 = (4,5,6)d3 = {tuple1:10,tuple2:20} #用tuple作为字典的keyprint(d3) list1 = [1,原创 2020-12-19 11:27:11 · 721 阅读 · 1 评论 -
Python错误集锦:使用list.index()提示ValueError: 20 is not in list
原文链接:http://www.juzicode.com/archives/2701错误提示:使用list的index方法查找元素时提示:ValueError: 20 is not in list#juzicode.com/vx:桔子codelst = [1,3,9,5,21]a = lst.index(3)print('3第1次出现的位置',a)a = lst.index(20)print('20第1次出现的位置',20)3第1次出现的位置 1--------------原创 2020-12-19 11:26:48 · 17251 阅读 · 1 评论 -
Python错误集锦:调用列表的元素时提示IndexError: list assignment index out of range
原文链接:http://www.juzicode.com/archives/2692错误提示:调用列表的元素时提示:IndexError: list assignment index out of range#juzicode.com/vx:桔子codelst = ['juzicode','香蕉','苹果','西瓜']lst[1] = '火龙果'print('lst:',lst)lst[4] = '百香果'print('lst:',lst)lst: ['juzicode', '原创 2020-12-19 11:27:21 · 1993 阅读 · 1 评论 -
Python错误集锦:除法运算时提示ZeroDivisionError: division by zero
原文链接:http://www.juzicode.com/archives/2669错误提示:进行除法运算时,提示ZeroDivisionError: division by zero#juzicode.com/vx:桔子codelst = [5,4,3,2,1,0]for l in lst: b = 10/l print('l=',l,'b=',b)l= 5 b= 2.0l= 4 b= 2.5l= 3 b= 3.3333333333333335l= 2 b=原创 2020-12-19 11:26:37 · 77826 阅读 · 2 评论 -
Python错误集锦:SyntaxError: invalid character in identifier
原文链接:http://www.juzicode.com/archives/2672错误提示:使用print()函数打印字符串,提示print后面的括号是无效字符:SyntaxError: invalid character in identifier#juzicode.comimport numpy as npa = np.arange(24)print('a.size=',a.size) File "<ipython-input-7-bc4a16c715d1>",原创 2020-12-18 19:45:09 · 1185 阅读 · 1 评论 -
Python错误集锦:subplot提示ValueError: Single argument to subplot must be a three-digit integer, not 3411
原文链接:http://www.juzicode.com/archives/2498错误提示:使用matplotlib画子图时提示:ValueError: Single argument to subplot must be a three-digit integer, not 3411Traceback (most recent call last): File "13-plt-subplot-子图限制.py", line 30, in <module> ax3 =原创 2020-12-18 19:46:45 · 6860 阅读 · 3 评论 -
Python错误集锦:打开文件路径提示参数无效,OSError: [Errno 22] Invalid argument: ‘D:\juzicode\桔子code\readme.txt’
原文链接:http://www.juzicode.com/archives/2493错误提示:打开文件路径提示参数无效:OSError: [Errno 22] Invalid argument: ‘D:\juzicode\桔子code\readme.txt’D:\juzicode>python read_file.pyTraceback (most recent call last): File "read_file.py", line 4, in <module>原创 2020-12-12 08:23:02 · 1459 阅读 · 2 评论 -
Python错误集锦:matplotlib legend提示:ValueError: Unrecognized location ‘upper’. Valid locations are
原文链接:http://www.juzicode.com/archives/2486错误提示:使用matplotlib legend提示:ValueError: Unrecognized location ‘upper’. Valid locations areTraceback (most recent call last): File "09-plt-step.py", line 30, in <module> plt.legend(title='参数where:'原创 2020-12-09 20:08:29 · 3069 阅读 · 4 评论 -
Python错误集锦:TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’
原文链接:http://www.juzicode.com/archives/2411错误提示:进行加减法运算时提示:TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’:D:\juzicode>python test.pyTraceback (most recent call last): File "test.py", line 6, in <module> c=a+bTypeErr..原创 2020-12-08 22:37:14 · 39085 阅读 · 0 评论 -
Python错误集锦:NameError: name ‘a’ is not defined
原文链接:http://www.juzicode.com/archives/2364错误提示:使用变量时提示NameError: name ‘a’ is not definedD:\juzicode>python test-not-define.py微信公众号: 桔子code / juzicode.comb= 5Traceback (most recent call last): File "test-not-define.py", line 5, in <module&原创 2020-12-06 20:29:22 · 20038 阅读 · 1 评论 -
Python错误集锦:IndentationError: unindent does not match any outer indentation level
原文链接:http://www.juzicode.com/archives/2258错误提示:提示在次级语句中和外层的缩进不匹配:IndentationError: unindent does not match any outer indentation level可能原因:1、for循环中的语句缩进格式不一致,虽然肉眼可见看到的缩进是一样的,但是实际上使用了tab代替4个空格。通过notepad++放开所有字符的显示,可以看到一个用的是tab键,一个用的是4个空格键:...原创 2020-11-25 19:36:13 · 742 阅读 · 0 评论 -
Python错误集锦:The repository located at pypi.douban.com is not a trusted or secure host and is being ig
原文链接:http://www.juzicode.com/archives/2243错误提示:使用命令:pip install numpy -i http://pypi.douban.com/simple/ 安装模块时提示不是安全的主机:The repository located at pypi.douban.com is not a trusted or secure host and is being ignored. If this repository is available via原创 2020-11-25 18:57:29 · 3917 阅读 · 0 评论 -
Python错误集锦:在安装模块过程中出现socket.timeout: The read operation timed out
原文链接:http://www.juzicode.com/archives/2238错误提示:在安装模块过程中提示:socket.timeout: The read operation timed out,并连续发生错误,pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host=’files.pythonhosted.org’, port=443): Read timed out. File "d:\p原创 2020-11-25 18:57:59 · 2543 阅读 · 1 评论 -
Python错误集锦: NameError: name ‘numpy’ is not defined
原文链接:http://www.juzicode.com/archives/2225错误提示:使用numpy.eye()函数时报NameError: name ‘numpy’ is not definedD:\juzicode>python mod.py 微信公众号: 桔子code / juzicode.com Traceback (most recent call last): File "mod.py", line 3, in n = numpy.eye(4)原创 2020-11-24 21:49:41 · 9049 阅读 · 0 评论 -
Python错误集锦:ModuleNotFoundError: No module named ‘numpy’
原文链接:http://www.juzicode.com/archives/2228错误提示:导入numpy模块,调用模块内的eye()函数,提示:Traceback (most recent call last):File “test.py”, line 2, inimport numpyModuleNotFoundError: No module named ‘numpy’可能原因:1、没有安装numpy模块。解决方法:1、使用pip instal...原创 2020-11-24 21:20:28 · 930 阅读 · 0 评论 -
Python错误集锦:>>> python hello.py File ““, line 1 python hello.py ^ SyntaxError: invalid syntax >>>
原文链接:http://www.juzicode.com/archives/2223错误提示:运行解释器,在提示符下面输入“python py文件名称”,提示“SyntaxError: invalid syntax”。>>> python hello.py File "<stdin>", line 1 python hello.py ^SyntaxError: invalid syntax>>>.原创 2020-11-24 21:11:56 · 2457 阅读 · 1 评论 -
Python错误集锦:TypeError: ‘tuple’ object does not support item assignment
原文链接:http://www.juzicode.com/archives/2217错误提示:修改tuple时,提示TypeError: ‘tuple’ object does not support item assignment可能原因:1、tuple内的元素是不可以修改的,除非tuple内部再嵌套了list,可以修改list内部的元素解决方法:1、tuple不能修改元素,无解。如果要修改元素,改用list类型。...原创 2020-11-24 21:05:56 · 2691 阅读 · 0 评论 -
Python错误集锦:for x in range(5) ^ SyntaxError: invalid syntax
原文链接:http://www.juzicode.com/archives/2208错误提示:在循环语句中提示语法错误:for x in range(5) ^ SyntaxError: invalid syntax可能原因:1、for语句的最后和下层语句之间,需要使用冒号分隔,表示是2个语句层次,同样的情况也出现在条件语言、函数定义、类定义等表示不同层级语句之间。解决方法:1、在第4行for语句最后增加冒号:...原创 2020-11-24 00:58:09 · 9500 阅读 · 1 评论 -
Python错误集锦:IndentationError: unexpected indent
原文链接:http://www.juzicode.com/archives/2201错误提示:运行py文件,提示:IndentationError: unexpected indent,提示在第3行遇到非预期的缩进。可能原因:1、同一个层级的代码行,必须保存缩进一致,上面的代码中第2、3行的层级一样,所以第2、3行的缩进要一致,print函数前面的空格数量要保持一致:解决方法:1、将第3行和第2行一样顶格写。...原创 2020-11-24 00:47:33 · 14480 阅读 · 0 评论