pythongraphlidar

databuffer += data_str TypeError: can only concatenate str (not "_io.TextIOWrapper") to str


I am trying to animate my data which are from LIDAR sensor, but I got this error while trying to animate it! May you help the problem, I am quite new for Python programming, thanks a lot!

This is the problem that I got:

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

These is the data set that I try to animate:

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

type of my data set is string!


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()

Solution

  • open creates a buffered reader. there are many kinds of buffered readers; in this case, it is a text buffered reader. The reader itself cannot be treated like a string, BUT, if you tell your code to read the contents, you will get the data type equivalent to the buffered reader (bytes from a BytesIO buffered reader, and string from a TextIOWrapper)

    I would read up a bit on buffered readers as it'll definitely come in handy down the line, here

    This code also demonstrates how you can use the buffered reader for your purposes (with some changes to variable names to match the variable type better):

    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)
    

    Essentially, you need to have data_buffer add the read version of the wrapper contents, so where you have databuffer += data_str, you should really be doing databuffer += data_str.read()