gpt4 book ai didi

python - databuffer += data_str 类型错误 : can only concatenate str (not "_io.TextIOWrapper") to str

转载 作者:太空宇宙 更新时间:2023-11-03 21:06:31 26 4
gpt4 key购买 nike

我正在尝试对来自 LIDAR 传感器的数据进行动画处理,但在尝试对其进行动画处理时出现此错误!希望您能帮助解决这个问题,我对Python编程很陌生,非常感谢!

这是我遇到的问题:

File "C:\Users\cemal\AppData\Local\Programs\Python\Python37-32\veritipleriogrenme.py", line 29, in animate databuffer += data_str TypeError: can only concatenate str (not "_io.TextIOWrapper") to 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

我的数据集类型是字符串!

<小时/>
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()

关于python - databuffer += data_str 类型错误 : can only concatenate str (not "_io.TextIOWrapper") to str,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55359728/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com