2
Hello, I’m new to Python and my question may be obvious, but I would like to better understand the workings of the string
; I am facing an error of execution of a function that I created and do not understand the behavior of the string of this case. The function takes from each variable of the "data" list the blanks and the text after the identification of a specific character ("#") using the function .split()
, .strip()
and .index()
, leaving only the first part, without spaces, of each element of the list (using [s:]), whose value is returned by the function.
Follows the code:
arquivo = "treee; hpot #NomesEspecificos"
tipo = "30;40;10 #Numeros Especificos"
arq = "tritrem; bitrem; rodotrem #VehicleNamesIn"
dados = [tipol,arq,arquivo,tipo]
def separador(s):
s = ''.join(s.split(' '))
tail = s[s.index('#'):]
head = s.strip(tail)
return head
dados2=[separador(s) for s in dados]
print(dados2)
After execution, this code returns:
['2168', 'tritrem;bitrem;rodotr', 'treee;hpot', '30;40;10']
But you should return:
['2168', 'tritrem;bitrem;rodotrem', 'treee;hpot', '30;40;10']
I have already made some changes in the formation of strings to achieve this demonstrated result, but if you run the variable "file" with the following change:
arquivo = "treee;hpot #CraneNameInput"
It will result in the same error identified in the output of the "Arq variable":
['2168', 'tritrem;bitrem;rodotr', ';hpo', '30;40;10']
I would like to understand the reason for this deviation and, if possible, a solution to this problem. I confess that I have tried a lot and have not yet solved. Thanks a lot.
Thanks for the suggestion!!
– C. Ussi