My question is the following, I must order a file so that the dates or the years are as follows (the file has more than 100,000 records)
Año=1999
Sala1=123
Sala2=341
Sala3=541
(and it would be the same with 2000)
The format given by the file is the following:
Superman-2006-4
Where the last number is the room (4) and then we have the year (2006), how should I search the file for all those with the same date (previously I took the date and room of all) and then add them so that do they look good on me?
Part of the code to get date and room:
archivo=open("Peliculas.txt","rt")
leer=archivo.readline()
mayor=0
menor=1996#esto es ya con el año sacado
cont=0
cont2=0
Sala1,Sala2,Sala3,Sala4,Sala5=0,0,0,0,0
while leer:# ACA BUSCO EL MENOR AÑO Y EL MAYOR AÑO PARA PODER ASI ORDENAR EL ARCHIVO
cont+=1
controlar=leer[::-1]
poner_print=controlar[:7]#por aca hay algun error que me caga en la ultima linea, revisarlo
poner_print=poner_print[::-1]#error solucionado con el cont que para en la linea 49 porque en la 50 surge un error
Año1=poner_print[:4]
Sala1=poner_print[5:]
if int(Año1)>mayor:
mayor=int(Año1)#saque el año aca
if int(Año1)<menor and cont!=49:
menor=int(Año1)#saque el menor año
leer=archivo.readline()
print("El año mas grande fue" ,mayor)
print("El año mas chico fue" ,menor)
In this case, the example I did to make it quick was 50 lines (the file)
Let's see if I understood correctly. You have a file called
"Peliculas.txt"
that contains for example lines like these:That is, each line contains a movie title, a year, and a number that I understand is a room, separated by hyphens.
What you want is to process that file to count in each year how many times each of the rooms appears, and then generate a report ordered by years and within each year ordered by rooms, correct?
If I got it right, the following would do what you're looking for. I have made use of a dictionary to put in it each year that I find. For each of those years, I create another dictionary within the previous one, in which the keys are the rooms, and the values are how many times I have seen that room appear in that year.
As you can see, I iterate through the lines of the file (I don't need to do
readline()
, the loopfor dato in f:
returns one line each time), I remove the carriage return withdato.strip()
and divide by the hyphen with.split("-")
, to separate into the three fields. Then I add the year if it was not already in the dictionary, and the room. Finally increased the value of the room that year.At the end of the loop, the dictionary
resumen
has what you are looking for, although it does not have any particular order, since in python dictionaries do not maintain order. For example, you could have something like:So that it comes out ordered and in the format you are looking for, the following loop goes through the dictionary using
sorted()
its keys, and prints its values:Example output:
Note
Since I didn't have your data, for testing I generated a random data file using the following code: