Python 数据缓冲区+;=数据类型错误:只能将str(而不是“io.TextIOWrapper”)连接到str
我试图动画我的数据是从激光雷达传感器,但我得到了这个错误,而试图动画它!希望你能帮我解决这个问题,我对Python编程很陌生,非常感谢 这就是我遇到的问题: 文件“C:\Users\cemal\AppData\Local\Programs\Python\Python37-32\VeritipleRogreName.py”,第29行,在animate databuffer+=data\u str TypeError:只能将str(而不是“\io.TextIOWrapper”)连接到str 以下是我尝试设置动画的数据集:Python 数据缓冲区+;=数据类型错误:只能将str(而不是“io.TextIOWrapper”)连接到str,python,graph,lidar,Python,Graph,Lidar,我试图动画我的数据是从激光雷达传感器,但我得到了这个错误,而试图动画它!希望你能帮我解决这个问题,我对Python编程很陌生,非常感谢 这就是我遇到的问题: 文件“C:\Users\cemal\AppData\Local\Programs\Python\Python37-32\VeritipleRogreName.py”,第29行,在animate databuffer+=data\u str TypeError:只能将str(而不是“\io.TextIOWrapper”)连接到str 以下是我
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
我的数据集的类型是字符串
open
创建缓冲读取器。有多种缓冲读取器;在这种情况下,它是一个文本缓冲读取器。读取器本身不能像字符串一样处理,但是,如果您告诉代码读取内容,您将获得与缓冲读取器等效的数据类型(字节来自BytesIO缓冲读取器,字符串来自TextIOWrapper)
我会读一些缓冲读卡器,因为它肯定会在接下来的时间里派上用场
这段代码还演示了如何使用缓冲读取器(对变量名称进行一些更改以更好地匹配变量类型):
本质上,您需要让data\u buffer添加包装内容的读取版本,因此在您有
databuffer+=data\u str
的地方,您应该真正执行databuffer+=data\u str.read()
open
创建一个缓冲读取器。有多种缓冲读取器;在这种情况下,它是一个文本缓冲读取器。读取器本身不能像字符串一样处理,但是,如果您告诉代码读取内容,您将获得与缓冲读取器等效的数据类型(字节来自BytesIO缓冲读取器,字符串来自TextIOWrapper)
我会读一些缓冲读卡器,因为它肯定会在接下来的时间里派上用场
这段代码还演示了如何使用缓冲读取器(对变量名称进行一些更改以更好地匹配变量类型):
本质上,您需要让data\u buffer添加包装内容的读取版本,因此在您有
databuffer+=data\u str
的地方,您应该真正执行databuffer+=data\u str.read()
您可以发布产生此错误的正在运行的代码吗?在看不到您正在做什么的情况下,我们只能真正猜测问题可能是什么。这会好一点,但在问题中粘贴代码文本将帮助更多!只需将其复制粘贴到问题的末尾,突出显示所有内容,然后按{}
按钮即可将其格式化。这让我们可以复制粘贴您必须的内容,以快速查看问题所在。如果我现在想尝试你的代码,我需要大量的输入。你能发布你正在运行的导致此错误的代码吗?在看不到您正在做什么的情况下,我们只能真正猜测问题可能是什么。这会好一点,但在问题中粘贴代码文本将帮助更多!只需将其复制粘贴到问题的末尾,突出显示所有内容,然后按{}
按钮即可将其格式化。这让我们可以复制粘贴您必须的内容,以快速查看问题所在。如果我现在想试试你的代码,我会有很多打字工作要做。
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()
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)