Read Chains Python

1

I need to obtain values that are contained in a string of type str separated by a "," and that when I find the comma, save that value in a new variable for example, I have the string:

10.90,500,56.99

and I want to save the values in a new variable a = 10.90 b = 500 c = 56.99

This is my code

import serial, time

launchpadPort=serial.Serial('COM6', 9600, timeout=1)

while True:
    #datosLaunchpad = open("datos.csv", 'a')
    getSerialValue = launchpadPort.readline()
    b = getSerialValue.decode('utf-8').rstrip('\n')
    print(f"Prueba: {b}")
    datosLaunchpad.write(b)
    print(type(b))
    
asked by Josue Hernandez 31.10.2018 в 08:32
source

6 answers

0

you can do something like this!

str="´.500.500, 652.2352, 956.2´"
n= [ float(x.lstrip(".")) if '.' in x else int(x.lstrip(".")) for x in (str[1:-1].split(",")) ]
a,b,c = n
print(n)
print(a,b,c)

the result would be:

[500.5, 652.2352, 956.2]
500.5 652.2352 956.2
    
answered by 31.10.2018 / 21:12
source
0

Have you tried replacing the lines:

getSerialValue = launchpadPort.readline()
b = getSerialValue.decode('utf-8').rstrip('\n')

for?

b = [int(x) for x in launchpadPort.readline().split()]
    
answered by 31.10.2018 в 08:54
0
def agregar(a):

    temp=""
    contador=0;
    pos=0
    vec=[]
    for c in a:
        pos=pos +1
        if c==",":
            contador=contador +1  #cuenta la cantidad de comas y la posicion 
            posfinal=pos #guarda la posicion de la ultima coma

    for x in a:    #recorremos cno x la cadena a 
        if x!=",":
            temp=temp + x    #cargamos en una variable temporal
        else:
            vec.append(temp)
            temp=""        #limpiamos la variable temporal para cargar el otro numero 

    #nos falta la ultima parte de la cadena 
    vec.append(a[posfinal:])

    for a in range(contador + 1):   #contador cuenta la cantidad - 1 de numeros entre comas 
        print vec[a]  

    return vec

lista=agregar("10.90,500,56.99")

# here you send the acdena you want to separate print list

    
answered by 31.10.2018 в 20:32
0

This could work for you:

b = b'10.90,500,56.99' # el valor de ejemplo que das en bytes
b_list = b.decode("utf-8").split(",") # convierte a texto y separa los elementos por ","
float_list = [float(x) for x in b_list] # convierte los elementos de la lista en float
    
answered by 01.11.2018 в 05:05
0

I'm going to assume that in "b" you have the string with the numbers

# con split(",") divides el string en una lista a partir de cada coma que encuentre
lista_numeros = [float(v) for v in b.split(",")]

# aquí vamos a comprobar el  tipo de variable
for i in lista_numeros:
    print(type(i))

I have not worked with Arduinos, but if this is worth it, you are commenting on what you need most and I am adding it.

    
answered by 01.11.2018 в 11:47
0

Try the following:

a= "10.85,56,3.21,89"
b= a.split(',')

Now b will have the following

["10.85", "56", "3.21", "89"]

You can access each value by indicating the position Ej b[0]="10.85" if you wanted it as float would be numero= float(b[0])

    
answered by 01.11.2018 в 19:45