Python - Poner Key con dato de txt

 
Vista:
Imágen de perfil de Santiago

Poner Key con dato de txt

Publicado por Santiago (1 intervención) el 23/05/2022 17:50:30
Buen día

Tengo que convertir un archivo txt a Json el archivo txt tiene este formato


txt

y use el siguiente codigo para hacerlo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# Python program to convert text
# file to JSON
 
 
import json
 
 
# the file to be converted
filename = 'Descarga.txt'
 
# resultant dictionary
dict1 = {}
 
# fields in the sample file
fields =['Skill','Staff','Disponibles','Llamadas en Espera','Agentes en AUX','Recibidas','Abandonadas']
 
with open(filename) as fh:
 
 
 
	# count variable for employee id creation
	l = 1
 
	for line in fh:
 
		# reading line by line from the text file
		description = list( line.strip().split(None, 7))
 
		# for output see below
		print(description)
 
		# for automatic creation of id for each employee
		sno ='Skl'+str(l)
 
		# loop variable
		i = 0
		# intermediate dictionary
		dict2 = {}
		while i<len(fields):
 
				# creating dictionary for each employee
				dict2[fields[i]]= description[i]
				i = i + 1
 
		# appending the record of each employee to
		# the main dictionary
		dict1[sno]= dict2
		l = l + 1
 
 
# creating json file
out_file = open("Descarga.json", "w")
json.dump(dict1, out_file, indent = 4)
out_file.close()

Sin embargo me lo entrega de la siguiente manera


Json

Quisiera que donde dice Skl1 trajera la primera columna osea el 401 quedando de la siguiente manera


JsonCorrecto

entiendo que eso debo hacerlo en la linea 33 pero no se como decirle que use el dato de la columna para poner la llave, espero me puedan ayudar

Gracias
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil
Val: 2.808
Oro
Ha mantenido su posición en Python (en relación al último mes)
Gráfica de Python

Poner Key con dato de txt

Publicado por tincopasan (1082 intervenciones) el 23/05/2022 21:23:30
tenés que cambiar varias líneas:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#-*- coding: utf  -8 -*-
# Python program to convert text
# file to JSON
 
import json
# the file to be converted
filename = 'Descarga.txt'
 
# resultant dictionary
dict1 = {}
 
# fields in the sample file
 
fields =['Staff','Disponibles','Llamadas en Espera','Agentes en AUX','Recibidas','Abandonadas']
with open(filename) as fh:
    # count variable for employee id creation
    l = 1
    for line in fh:
        # reading line by line from the text file
        description = list( line.strip().split(None, 7))
        # for output see below
        print(description)
        # for automatic creation of id for each employee
        sno =description[0]
        # loop variable
        i = 0
        # intermediate dictionary
        dict2 = {}
        while i<len(fields):
            # creating dictionary for each employee
            dict2[fields[i]]= description[i+1]
            i = i + 1
        # appending the record of each employee to
        # the main dictionary
        dict1[sno]= dict2
        l = l + 1
 
# creating json file
out_file = open("Descarga.json", "w")
json.dump(dict1, out_file, indent = 4)
out_file.close()

Creo que con esos cambios obtenés el resultado que buscas.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar