我正在尝试对来自LIDAR传感器的数据进行动画处理,但是在尝试对其进行动画处理时出现了此错误!可以帮您解决这个问题,对于Python编程我还是很陌生,非常感谢!
这是我遇到的问题:
文件“ C:\ Users \ cemal \ AppData \ Local \ Programs \ Python \ Python37-32 \ veritipleriogrenme.py”,第29行,位于动画数据缓冲区+ = data_str中TypeError:只能将str(而不是“ _io.TextIOWrapper”)连接到力量
这些是我尝试制作动画的数据集:
0.0,0.0
0.0,269.1
0.0,270.3
0.0,271.5
1617.8,265.6
1627.3,266.8
1629.0,268.0
1633.0,269.2
我的数据集类型是字符串!
import matplotlib.pyplot as plot
import math
from matplotlib import style
import matplotlib.animation as animation
import numpy as np
fig=plot.figure(figsize=(4,4))
ax = fig.add_subplot(111, projection='polar')
ax.set_ylim(0,2000)
data = np.zeros(360)
theta = np.linspace(0,360, num=360)
l, = ax.plot([],[])
databuffer = ""
uzaklik = np.zeros(360)
pol = np.linspace(0,360, num=360)
def animate(i):
global data, databuffer
data_str = open(r"C:\Users\cemal\OneDrive\Masaüstü\veri2.txt","r")
databuffer += data_str
aci=np.linspace(0,360, num=360)
cap=np.zeros(360)
p_pol=np.linspace(0,360, num=360)
p_uzaklik=np.zeros(360)
aci2=np.linspace(0,360, num=360)
cap=np.zeros(360)
for x in data_str:
pol =x.partition(",")[2].rstrip()
uzaklik =x.split(',')[0]
try:
p_pol=float(pol.strip().strip("'"))
p_uzaklik=float(uzaklik.strip().strip("'"))
aci=np.append(p_pol)
cap=np.append(p_uzaklik)
aci2=[math.radians(i) for i in aci]
l.set_data(cap, aci2 )
data_buffer=""
return l,
except ValueError:
continue
ani = animation.FuncAnimation(fig, animate,interval=10000)
plot.show()
最佳答案
open
创建一个缓冲的读取器。有多种缓冲的阅读器;在这种情况下,它是文本缓冲的阅读器。读取器本身不能像字符串一样对待,但是,如果您告诉代码读取内容,则将获得与缓冲读取器等效的数据类型(来自BytesIO缓冲读取器的字节,以及来自TextIOWrapper的字符串)
我会在缓冲的阅读器上阅读一些内容,因为它肯定会派上用场,here
此代码还演示了如何根据需要使用缓冲的读取器(对变量名进行了一些更改以更好地匹配变量类型):
data_buffer = ""
data_str_wrapper = open(r"C:\Users\cemal\OneDrive\Masaüstü\veri2.txt","r")
try:
str += data_str_wrapper
except Exception as e:
print("Can't combine strings and wrappers")
print(e)
data_buffer += data_str_wrapper.read()
print("Now that i've read the buffer, I can treat it like a string")
print(data_buffer)
本质上,您需要让data_buffer添加包装内容的读取版本,因此在
databuffer += data_str
的地方,您实际上应该在做databuffer += data_str.read()