Python - Recorrer un array de diccionarios para obtener unos valores

 
Vista:
Imágen de perfil de ignacio
Val: 31
Ha aumentado su posición en 18 puestos en Python (en relación al último mes)
Gráfica de Python

Recorrer un array de diccionarios para obtener unos valores

Publicado por ignacio (8 intervenciones) el 02/09/2020 21:54:39
Tenemos un archivo de entrada pelis.json con datos de películas obtenido de http://www.omdbapi.com/
y lo recorreremos para obtener los siguientes datos:
Nombre
Actor o actriz principal
Rating de Rotten Tomatoes
Recaudación

Esos datos serán grabados en un nuevo archivo, llamado pelis.txt, cuya estructura será la siguiente:

Nombre, Actor, Rating, Recaudación
Peli-1, Actor-1, 78, 123456789
Peli-2, Actriz-1, 91, 987654321

Luego de ello, con otro programa (o bien el mismo con otra función en el menú) se leerá pelis.txt y se mostrará por pantalla el Rating promedio y la suma de las recaudaciones de todas las películas.

Nota: Para leer el archivo pelis.json, solamente puede usar el método read (no se permite readline ni readlines). Además serán de mucha ayuda el uso de los métodos seek y tell.
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
Imágen de perfil de xve
Val: 2.239
Plata
Ha mantenido su posición en Python (en relación al último mes)
Gráfica de Python

Recorrer un array de diccionarios para obtener unos valores

Publicado por xve (1646 intervenciones) el 02/09/2020 22:08:29
Adjunto el JSON con los datos que me lo ha dado Ignacio en el chat:

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
[
    {
        "Title": "Arrival",
        "Year": "2016",
        "Rated": "PG-13",
        "Released": "11 Nov 2016",
        "Runtime": "116 min",
        "Genre": "Drama, Mystery, Sci-Fi, Thriller",
        "Director": "Denis Villeneuve",
        "Writer": "Eric Heisserer (screenplay by), Ted Chiang (based on the story \"Story of Your Life\" written by)",
        "Actors": "Amy Adams, Jeremy Renner, Forest Whitaker, Michael Stuhlbarg",
        "Plot": "A linguist works with the military to communicate with alien lifeforms after twelve mysterious spacecraft appear around the world.",
        "Language": "English, Russian, Mandarin",
        "Country": "USA, Canada",
        "Awards": "Won 1 Oscar. Another 67 wins & 263 nominations.",
        "Poster": "https://m.media-amazon.com/images/M/MV5BMTExMzU0ODcxNDheQTJeQWpwZ15BbWU4MDE1OTI4MzAy._V1_SX300.jpg",
        "Ratings": [
            {
                "Source": "Internet Movie Database",
                "Value": "7.9/10"
            },
            {
                "Source": "Rotten Tomatoes",
                "Value": "94%"
            },
            {
                "Source": "Metacritic",
                "Value": "81/100"
            }
        ],
        "Metascore": "81",
        "imdbRating": "7.9",
        "imdbVotes": "575,270",
        "imdbID": "tt2543164",
        "Type": "movie",
        "DVD": "14 Feb 2017",
        "BoxOffice": "$100,501,349",
        "Production": "21 Laps Entertainment",
        "Website": "N/A",
        "Response": "True"
    },
    {
        "Title": "Transcendence",
        "Year": "2014",
        "Rated": "PG-13",
        "Released": "18 Apr 2014",
        "Runtime": "119 min",
        "Genre": "Action, Drama, Sci-Fi, Thriller",
        "Director": "Wally Pfister",
        "Writer": "Jack Paglen",
        "Actors": "Johnny Depp, Rebecca Hall, Paul Bettany, Cillian Murphy",
        "Plot": "A scientist's drive for artificial intelligence, takes on dangerous implications when his consciousness is uploaded into one such program.",
        "Language": "English",
        "Country": "UK, China, USA",
        "Awards": "3 wins & 5 nominations.",
        "Poster": "https://m.media-amazon.com/images/M/MV5BMTc1MjQ3ODAyOV5BMl5BanBnXkFtZTgwNjI1NDQ0MTE@._V1_SX300.jpg",
        "Ratings": [
            {
                "Source": "Internet Movie Database",
                "Value": "6.3/10"
            },
            {
                "Source": "Rotten Tomatoes",
                "Value": "19%"
            },
            {
                "Source": "Metacritic",
                "Value": "42/100"
            }
        ],
        "Metascore": "42",
        "imdbRating": "6.3",
        "imdbVotes": "212,957",
        "imdbID": "tt2209764",
        "Type": "movie",
        "DVD": "22 Jul 2014",
        "BoxOffice": "$23,014,504",
        "Production": "Warner Bros. Pictures",
        "Website": "N/A",
        "Response": "True"
    },
    {
        "Title": "Serenity",
        "Year": "2005",
        "Rated": "PG-13",
        "Released": "30 Sep 2005",
        "Runtime": "119 min",
        "Genre": "Action, Adventure, Sci-Fi, Thriller",
        "Director": "Joss Whedon",
        "Writer": "Joss Whedon",
        "Actors": "Nathan Fillion, Gina Torres, Alan Tudyk, Morena Baccarin",
        "Plot": "The crew of the ship Serenity try to evade an assassin sent to recapture one of their members who is telepathic.",
        "Language": "English, Mandarin",
        "Country": "USA",
        "Awards": "9 wins & 9 nominations.",
        "Poster": "https://m.media-amazon.com/images/M/MV5BOWE2MDAwZjEtODEyOS00ZjYyLTgzNDUtYmNiY2VmNWRiMTQxXkEyXkFqcGdeQXVyNTIzOTk5ODM@._V1_SX300.jpg",
        "Ratings": [
            {
                "Source": "Internet Movie Database",
                "Value": "7.8/10"
            },
            {
                "Source": "Rotten Tomatoes",
                "Value": "82%"
            },
            {
                "Source": "Metacritic",
                "Value": "74/100"
            }
        ],
        "Metascore": "74",
        "imdbRating": "7.8",
        "imdbVotes": "279,329",
        "imdbID": "tt0379786",
        "Type": "movie",
        "DVD": "20 Dec 2005",
        "BoxOffice": "$25,335,935",
        "Production": "Universal Pictures",
        "Website": "N/A",
        "Response": "True"
    }
]
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
Imágen de perfil de xve
Val: 2.239
Plata
Ha mantenido su posición en Python (en relación al último mes)
Gráfica de Python

Recorrer un array de diccionarios para obtener unos valores

Publicado por xve (1646 intervenciones) el 02/09/2020 22:19:48
Esto es lo que te he publicado en el chat

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
valores=[
    {
        "Title": "Arrival",
        "Year": "2016",
        "Rated": "PG-13",
        "Released": "11 Nov 2016",
        "Runtime": "116 min",
        "Genre": "Drama, Mystery, Sci-Fi, Thriller",
        "Director": "Denis Villeneuve",
        "Writer": "Eric Heisserer (screenplay by), Ted Chiang (based on the story \"Story of Your Life\" written by)",
        "Actors": "Amy Adams, Jeremy Renner, Forest Whitaker, Michael Stuhlbarg",
        "Plot": "A linguist works with the military to communicate with alien lifeforms after twelve mysterious spacecraft appear around the world.",
        "Language": "English, Russian, Mandarin",
        "Country": "USA, Canada",
        "Awards": "Won 1 Oscar. Another 67 wins & 263 nominations.",
        "Poster": "https://m.media-amazon.com/images/M/MV5BMTExMzU0ODcxNDheQTJeQWpwZ15BbWU4MDE1OTI4MzAy._V1_SX300.jpg",
        "Ratings": [
            {
                "Source": "Internet Movie Database",
                "Value": "7.9/10"
            },
            {
                "Source": "Rotten Tomatoes",
                "Value": "94%"
            },
            {
                "Source": "Metacritic",
                "Value": "81/100"
            }
        ],
        "Metascore": "81",
        "imdbRating": "7.9",
        "imdbVotes": "575,270",
        "imdbID": "tt2543164",
        "Type": "movie",
        "DVD": "14 Feb 2017",
        "BoxOffice": "$100,501,349",
        "Production": "21 Laps Entertainment",
        "Website": "N/A",
        "Response": "True"
    },
    {
        "Title": "Transcendence",
        "Year": "2014",
        "Rated": "PG-13",
        "Released": "18 Apr 2014",
        "Runtime": "119 min",
        "Genre": "Action, Drama, Sci-Fi, Thriller",
        "Director": "Wally Pfister",
        "Writer": "Jack Paglen",
        "Actors": "Johnny Depp, Rebecca Hall, Paul Bettany, Cillian Murphy",
        "Plot": "A scientist's drive for artificial intelligence, takes on dangerous implications when his consciousness is uploaded into one such program.",
        "Language": "English",
        "Country": "UK, China, USA",
        "Awards": "3 wins & 5 nominations.",
        "Poster": "https://m.media-amazon.com/images/M/MV5BMTc1MjQ3ODAyOV5BMl5BanBnXkFtZTgwNjI1NDQ0MTE@._V1_SX300.jpg",
        "Ratings": [
            {
                "Source": "Internet Movie Database",
                "Value": "6.3/10"
            },
            {
                "Source": "Rotten Tomatoes",
                "Value": "19%"
            },
            {
                "Source": "Metacritic",
                "Value": "42/100"
            }
        ],
        "Metascore": "42",
        "imdbRating": "6.3",
        "imdbVotes": "212,957",
        "imdbID": "tt2209764",
        "Type": "movie",
        "DVD": "22 Jul 2014",
        "BoxOffice": "$23,014,504",
        "Production": "Warner Bros. Pictures",
        "Website": "N/A",
        "Response": "True"
    },
    {
        "Title": "Serenity",
        "Year": "2005",
        "Rated": "PG-13",
        "Released": "30 Sep 2005",
        "Runtime": "119 min",
        "Genre": "Action, Adventure, Sci-Fi, Thriller",
        "Director": "Joss Whedon",
        "Writer": "Joss Whedon",
        "Actors": "Nathan Fillion, Gina Torres, Alan Tudyk, Morena Baccarin",
        "Plot": "The crew of the ship Serenity try to evade an assassin sent to recapture one of their members who is telepathic.",
        "Language": "English, Mandarin",
        "Country": "USA",
        "Awards": "9 wins & 9 nominations.",
        "Poster": "https://m.media-amazon.com/images/M/MV5BOWE2MDAwZjEtODEyOS00ZjYyLTgzNDUtYmNiY2VmNWRiMTQxXkEyXkFqcGdeQXVyNTIzOTk5ODM@._V1_SX300.jpg",
        "Ratings": [
            {
                "Source": "Internet Movie Database",
                "Value": "7.8/10"
            },
            {
                "Source": "Rotten Tomatoes",
                "Value": "82%"
            },
            {
                "Source": "Metacritic",
                "Value": "74/100"
            }
        ],
        "Metascore": "74",
        "imdbRating": "7.8",
        "imdbVotes": "279,329",
        "imdbID": "tt0379786",
        "Type": "movie",
        "DVD": "20 Dec 2005",
        "BoxOffice": "$25,335,935",
        "Production": "Universal Pictures",
        "Website": "N/A",
        "Response": "True"
    }
]
 
resultado=[[i["Title"], i["Actors"], i["BoxOffice"]] for i in valores]
 
print(resultado)
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