Problem with reading files in python

0

I have some doubts regarding the issue of working with files in python since I am starting on this topic. Is that I have to make a program that reads lines of numbers separated by ":" from a file .txt in this case of comunicaciones.txt that has this content: (It has enough more but for not making a post too big)

Tarifa 3

08:48:45

10:30:54

07:33:11

23:58:10

05:10:18

14:52:43

05:25:47

06:10:05

03:03:25

08:07:30

And then display on screen Rate 0 and the first line of numbers in this case: 08:48:45 where 08 are the hours 48 minutes 45 seconds and with a function previously calculated its price based on a rate per second (royo telephony).

THIS IS WHAT I HAVE OF CODE:

lista=[]

def pasar_a_segundos(horas,minutos,segundos):
    pasarhoras=horas*3600
    pasarminutos=minutos*60
    segundos=pasarhoras+pasarminutos+segundos
    return segundos


def calcular_coste(calculoseg,tarifa):
    precio=calculoseg*tarifa
    return precio


def convertir_a_euros(precio):
    eurofinal=round(precio/100,2)
    return eurofinal



abrir=open("comunicaciones.txt","r")
type(abrir)

with open("comunicaciones.txt","r") as fichero:
    primera=True
    for linea in fichero:
        if primera:

        else:
            linea=linea.split(":")
            horas=int(lista[0])
            minutos=int(lista[1])
            segundos=int(lista[2])

print(listalineas)
print(len(listalineas))

I assume that we have to separate the values of the ":" but then I am not able to pass those values to seconds and then calculate the price of all the seconds in .

Sorry if I explain myself wrong but it is quite complicated, I leave you the problem statement to ensure the help.

  

Make a program that tells you how much each communication is worth and the total money of all communications. This time the data of the duration of communications and the rate per seconds are in this file where in the first line you find the rate, and in the remaining the duration of each of the communications expressed in hours, minutes and seconds .

    
asked by ShJod 18.12.2017 в 18:49
source

1 answer

0

What you have to do is verify that after separating there are 3 elements and these are digits, then you just have to unpack them and convert them to integers to make the necessary operations:

with open("comunicaciones.txt","r") as fichero:
    for linea in fichero:
        linea=linea.strip().split(":")
        if len(linea) == 3 and all(c.isdigit() for c in linea):
            hora, minuto, segundo = map(int, linea)
            segundos = pasar_a_segundos(hora, minuto, segundo)
            print(segundos)
    
answered by 18.12.2017 в 19:02