Python Forum

Full Version: help to parser arguments list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi!
In my python I need to parse the argument list like these:
"call_type: "I" alert_id: "101 " data_center: "TOTAL " memname: " " order_id: "0000c" severity: "V" status: "Not_Noticed " send_time: "20250113232733" last_user: " " last_time: " " message: "Ended not OK" run_as: "admin " sub_application: "subapp " application: "app " job_name: "JOB_FALLIDO " host_id: "em2 " alert_type: "R" closed_from_em: " " ticket_number: " " run_counter: "00014 "" 
I'm trying with argparse but some filed is not well recognized:
def parse_args():
    # Crear el parser
    parser = argparse.ArgumentParser(description="Parsear los argumentos en formato clave-valor")

    # Definir los argumentos esperados (aunque no se sabe el nĂºmero exacto, usaremos un enfoque flexible)
    parser.add_argument('args', nargs='+', help="Lista de argumentos clave:valor")

    # Parsear los argumentos
    return parser.parse_args()
It shows me:

2025-01-13 23:30:00 - INFO - call_type: I alert_id: 97
2025-01-13 23:30:00 - INFO -  data_center: TOTAL
2025-01-13 23:30:00 - INFO -  memname: 
2025-01-13 23:30:00 - INFO -  order_id: 0000c severity: V status: Not_Noticed
2025-01-13 23:30:00 - INFO -  send_time: 20250113225049 last_user: 
2025-01-13 23:30:00 - INFO -  last_time: 
2025-01-13 23:30:00 - INFO -  message: Ended
2025-01-13 23:30:00 - INFO - OK run_as: admin
2025-01-13 23:30:00 - INFO -  sub_application: subapp
2025-01-13 23:30:00 - INFO -  application: app
2025-01-13 23:30:00 - INFO -  job_name: JOB_FALLIDO
2025-01-13 23:30:00 - INFO -  host_id: em2
2025-01-13 23:30:00 - INFO -  alert_type: R closed_from_em: 
2025-01-13 23:30:00 - INFO -  ticket_number: 
2025-01-13 23:30:00 - INFO -  run_counter: 00010
I need call_type, alert_id, order_id, serverity, status, last_user and so on separated.
Any idea?
Thanks
The problem is not in the script, it is in the way you invoke the script. How do you invoke the script? Do you write a command line in a Cmd window, or do you launch the script from another one ? How exactly? Which syntax are you using?
Hi absolut,

I don't know from where come you input but it is not consistent.

some time string between " " start or end or both, with a space Sick

If you created this formatting may you can use something better to generate this input (see yaml, json etc..)

if you don't have hand on the input format, you don;t necessary need to relay on a parser, you can use all the string method and python basic built-in to manage this.

Let us know if you kneed more guidance.

Cheers
Would something like this help you?
Note: I changed the enclosing double quotes to single quotes.

def parse_data_string (data_string) -> list :
	data_array = []
	collector = ""
	flag = False
	for element in data_string :
		collector += element 
		if element == '"' :
			if flag :
				if collector [0] == " " :
					collector = collector [1:]
				data_array.append (collector)
				collector = ""
				flag = False
			else : flag = True
	return data_array

data_array = parse_data_string ('call_type: "I" alert_id: "101 " data_center: "TOTAL " memname: " " order_id: "0000c" severity: "V" status: "Not_Noticed " send_time: "20250113232733" last_user: " " last_time: " " message: "Ended not OK" run_as: "admin " sub_application: "subapp " application: "app " job_name: "JOB_FALLIDO " host_id: "em2 " alert_type: "R" closed_from_em: " " ticket_number: " " run_counter: "00014 "')
for element in data_array :
	print (element)
Output:
call_type: "I" alert_id: "101 " data_center: "TOTAL " memname: " " order_id: "0000c" severity: "V" status: "Not_Noticed " send_time: "20250113232733" last_user: " " last_time: " " message: "Ended not OK" run_as: "admin " sub_application: "subapp " application: "app " job_name: "JOB_FALLIDO " host_id: "em2 " alert_type: "R" closed_from_em: " " ticket_number: " " run_counter: "00014 "