[Tutor] How to change the values in python code?

Mats Wichmann mats at wichmann.us
Wed Nov 2 19:48:07 EDT 2022


On 11/2/22 13:39, samira khoda wrote:
> Hi
> 
> Could you please help me to understand why the code below does not work? I
> am trying to read a txt.file line by line that has integer timestamp
> values, the last number should be compared to the previous number.
> 
> And then split it where the value of the last is less than previous. But
> when I run the code prints 0 for previous in each line which technically
> says should not be split anywhere. I did a manual check up and I know there
> are various spots that the split should happen.
> 
> How can I make the value of the previous update to the value of the
> previous timestamp and not stay zero for every line?
> 
> Thanks in advance
> 
> Sam
> 
> Import pandas as pd
> previous=0
> i=0
> data=open('time-.txt', mode="r")
> 
> for line in data:
> 
>      x=line.split(", ")
>      last=int(x[7])
> 
> if last < previous :

as long as "last" can't be negative, it can never be less than previous, 
since you initialized previous to 0.

>      print("split here"+i)
>      previous=last
>      i=i+1
> 
>   #end the loop
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list