Arguments python as a string

0

I'm stuck in a script. I try to expose you to see if you can help me.

I define the following before entering the loop.

  

client, date, time, state, name, result, most_repeated = sys.argv [1] .split ()

     

timestamp = str (date) + '' + '' + str (time)

     

CUSTOMER 08-01-2018 08:44:00 1 Test1 ALERT 4000 192.168.1.113 == > > 8.8.8.8:53 == > > DNS

The execution is as follows:

./script.py "CLIENTE 08-01-2018 08:44:00 1 nombre de la alerta que sea 5000 192.168.1.113 ==>> 8.8.8.8:53 ==>> DNS"

By putting spaces between the parameters this takes them as different parameters as we all know.

I want it:

specifically the fields

./script.py "CLIENTE 08-01-2018 08:44:00 1 nombre de la alerta que sea 5000 192.168.1.113 ==>> 8.8.8.8:53 ==>> DNS"

I took it as a single parameter.

I've been formatting it with "% s" But when more words come out in the name of the alert it will not work.

Greetings and thanks in advance.

    
asked by Zero22 08.01.2018 в 16:51
source

1 answer

0

You can solve this by using star variables *var so that you save all the other entries in a variable, for your case it would be something like that.

import sys

cliente, fecha, hora, state, *otros= sys.argv[1].split()

print(cliente, fecha, hora, state, otros)

# CLIENTE 08-01-2018 08:44:00 1 ['nombre', 'de', 'la', 'alerta', 'que', 'sea', '5000', '192.168.1.113', '==>>', '8.8.8.8:53', '==>>', 'DNS']

You can see that the variable otros is a list with all the other entries, if we repeat this idea now unpacking this variable we can obtain the other values.

*nombre, result, ip1, _, ip2, _, most_repeated = otros

print(nombre, result, ip1, ip2, most_repeated)

# ['nombre', 'de', 'la', 'alerta', 'que', 'sea'] 5000 192.168.1.113 8.8.8.8:53 DNS

In this second step we use the star variable at the beginning, and we define the last variables to be taken, doing this now the variable name is in a list and the other parameters in independent variables.

And now you can only take the name by joining this list

nombre = ' '.join(nombre)
    
answered by 18.03.2018 / 21:38
source