python bug 小记录

it2022-05-05  140

1.File "F:\Experiments\ChineseNER-master\data_utils.py", line 293, in sort_and_pad batch_data.append(self.pad_data(sorted_data[i*batch_size : (i+1)*batch_size])) TypeError: slice indices must be integers or None or have an __index__ method TypeError: slice indices must be integers or None or have an index method

“[]”中的数据变成了浮点数,不能作为数组的下标了,需要将数据强制转换为int整型类型。即int(你要转换的数据) 在错误行源代码下加了转换:

batch_data.append(self.pad_data(sorted_data[int(i*batch_size): int((i+1)*batch_size)]))

2.import cPickle ModuleNotFoundError: No module named 'cPickle'

python2中是cPickle,python3中是pickle

3.未解决 已卒

我做了的事情:

~$ which qmake /opt/Qt5.6.0/5.6/gcc_64/bin/qmake ~$ qmake -version QMake version 3.0 Using Qt version 5.6.0 in /opt/Qt5.6.0/5.6/gcc_64/lib ~$ echo $PATH /opt/Qt5.6.0/5.6/gcc_64/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

可是在which qmake就没有任何反应。。不死心的我继续了qmake -version,不出意外的失败了,产生了深深的怀疑:是不是我的云服务器上根本没有安装Qt相关。。明天再搞吧 头大

4.ERROR:visdom:[WinError 10061] 由于目标计算机积极拒绝,无法连接。

我打开了两个命令提示框一个cmd,一个是anaconda prompt 在prompt里输入python -m visdom.server 然后再在cmd中运行代码就行了

5.loss += neg_log_likelihood.data[0] / len(data['words']) IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number

源代码:loss += neg_log_likelihood.data[0] / len(data['words']) 修改为:loss += neg_log_likelihood.item() / len(data['words'])

6.TypeError: a bytes-like object is required, not 'str'

在这里,python3和Python2在套接字返回值解码上有区别。解决办法非常的简单,只需要用上python的bytes和str两种类型转换的函数encode()、decode()即可! str通过encode()方法可以编码为指定的bytes,反过来,如果我们从网络或磁盘上读取了字节流,那么读到的数据就是bytes。要把bytes变为str,就需要用decode()方法。 源码为:with open(predf, 'wb') as f: f.write('\n'.join(prediction)) 修改为:with open(predf, 'wb') as f: f.write(('\n'.join(prediction)).encode())

7.AttributeError: 'dict' object has no attribute 'has_key'

python3中没有has_key了 源代码:if word2vec.has_key(word): embedding_pre.append(word2vec[word]) 修改代码如下:if word in word2vec: embedding_pre.append(word2vec[word])

8.有关linecache

with open(segment_tag_path,"r+", encoding='utf-8') as file_objtrain: # 传入已经标记好的分词文件 t_lines = file_objtrain.readlines() count = len(t_lines) for i in range(1, count+1): t1_line = linecache.getline(seg,i) # 按照索引i按行为单位读取txt文本

使用linecache去获取txt文本,我需要批量处理文本,所以此处的t1_line 在调不同txt时的值应该不同,但每当代码自动处理第二个文本时,就会报字符串超出索引的错误,打印输出t1_line 发现第二次的输出和第一次一样,也就是说对不同txt t1_line的值没有做出改变!困扰很久,才发现linecache语句需要清除缓存啊!!每次迭代后清除缓存就OK了!!我的天…

linecache.clearcache() # 清除缓存,否则会不断调第一次存入的字符串

最新回复(0)