TypeError: Unsupported type in write()解决办法

准备使用xlsxwriter把数据写入到excel里面,报错:

Traceback (most recent call last):
File "XXXXXXXXXX.py", line 134, in <module>
worksheet.write(startnum+1, 1, head_pose.position)
File "XXXXXXXXXX.py", line 85, in cell_wrapper
return method(self, *args, **kwargs)
File "XXXXXXXXXX.py", line 445, in write
return self._write(row, col, *args)
File "XXXXXXXXXX.py", line 517, in _write
raise TypeError("Unsupported type %s in write()" % type(token))
TypeError: Unsupported type <class 'numpy.ndarray'> in write()

检查后发现问题出在数据结构上,比如原程序中,这种数组形式的数据[ 0.0589693 0.05704604 -0.13406745]是不能直接写入到excel里面的

需要先进行一个数据的提取

提取方法是新建一个变量,然后把数组中每一个值赋值给变量

这样就可以导出了,比如原数据:

head_pose.position = [ 0.0589693 0.05704604 -0.13406745]

要这么操作:

head_posePosition001 = head_pose.position[0]
head_posePosition002 = head_pose.position[1]
head_posePosition003 = head_pose.position[2]
然后再把head_posePosition001、head_posePosition002、head_posePosition003三个数据分别写入excel:
worksheet.write(startnum+1, 1, head_pose.position)
worksheet.write(startnum+1, 2, head_pose.position)
worksheet.write(startnum+1, 3, head_pose.position)
这样就可以了

Python中如何print一些变量值

通过print(f'XXXXXXXX{变量、字符串}xxxxx')即可,f表示格式化字符串,格式化以后,花括号里面的变量或者字符串就能够用了

比如:

变量IPPORT,要输出一句话:

位于0.0.0.0的服务端启动成功,在50000端口等待客户链接

那么可以通过print(f来写:

print(f'位于{IP}的服务端启动成功,在{PORT}端口等待客户链接')
也可以写下面这样子,但是麻烦且不美观:

print('位于'+str(IP)+'的服务端启动成功,在'+str(PORT)+'端口等待客户链接')

opencv出现Can't parse 'center'. Sequence item with index 0 has a wrong type问题

程序报错退出,错误提示:

M = cv2.getRotationMatrix2D(eyesCenter, angle, scale)
TypeError: Can't parse 'center'. Sequence item with index 0 has a wrong type

查询后发现问题出在这行:

        eyesCenter = (leftEyeCenter[0] + rightEyeCenter[0] // 2,
            leftEyeCenter[1] + rightEyeCenter[1] // 2)
好像是由于软件包版本不兼容导致的
修改为:
        eyesCenter = (leftEyeCenter[0] + rightEyeCenter[0] // 2.0,
            leftEyeCenter[1] + rightEyeCenter[1] // 2.0)
即可完美运行!