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

samira samiraeastcoast at gmail.com
Mon Nov 7 15:43:43 EST 2022


Hi Alan

Thanks for the suggestions. I tried to address what you mentioned but I am
not sure if I understood it correctly as the codes are still not outputting
what I am looking for. I am also very new to coding so understanding some
of the python language is challenging. Thanks for the patience in advance.

Below is the example of data you have requested. As you can see there are
changes in numbers that are less than previous in multiple locations and
that is where I want the python to print "split here" so that I can split
the file and write to a new file later once I can see where splits happen.
Hope that helps

Line1: -1.75, 1.08, 10.35, -0.10, -0.01, -0.01, 23.19, 488

Line2: -1.75, 1.12, 10.39, -0.10, -0.01, -0.01, 23.20, 521

Line4: -1.75, 1.10, 10.40, -0.10, -0.00, -0.01, 23.30, 967

Line5: -1.77, 1.09, 10.40, -0.10, -0.01, -0.01, 23.28, 1000

Line6: -1.72, 1.08, 10.40, -0.10, -0.00, -0.01, 23.31, 1032



Line1340: -1.97, 0.84, 10.35, -0.10, -0.01, -0.01, 24.16, 48040

Line1341: -1.97, 0.86, 10.35, -0.10, -0.01, -0.01, 24.16, 48075


****this is where the split needs to happen where Last<previous

Line1342: -2.16, 0.54, 10.25, -0.10, -0.00, -0.01, 24.18, 493

Line1343: -2.20, 0.54, 10.34, -0.10, -0.01, -0.01, 24.17, 528



previous=0

with open ('test-copy.txt', mode="r") as timestamp:

    for line in timestamp:
          x=line.split(", ")
    last=int(x[7])
    print(last)
    print("last = ", last)
    print("previous = ", previous)

    if last < previous :
        print("split here")


Output is as below:

15339495
last =  15339495
previous =  0


Thanks

Sam



On Wed, Nov 2, 2022 at 9:05 PM Alan Gauld via Tutor <tutor at python.org>
wrote:

> On 02/11/2022 19:39, samira khoda wrote:
>
> > And then split it where the value of the last is less than previous. But
> > when I run the code prints 0 for previous
>
> You never print 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.
>
>
> It might help if you post a few lines of sample data including some
> lines where the last < previous and others where it is not.
> >
> > How can I make the value of the previous update to the value of the
> > previous timestamp and not stay zero for every line?
>
> There are seeral issues in your code:
>
> > Import pandas as pd
>
> That should be lower ase import. Please post actual code not
> retyped. It avoids wasting time on typos that don't exist
> in the real thing.
>
> > previous=0
> > i=0
> > data=open('time-.txt', mode="r")
>
> Notice that there is no value for last at this point.
>
> > for line in data:
> >     x=line.split(", ")
> >     last=int(x[7])
>
> You assign it here but if the file were empty it would not exist.
>
> However, as written, this is your complete loop.
> You simply iterate over each line in the file then exit the
> loop with last assigned to the 8th field of the last line.
> What does the last line of your data look like?
> It must have at least 8 fields or you'd get an error...
> But the fields could be from any old string. Of course there
> must be a number or the int() call would fail. But is it valid?
>
> > if last < previous :
>
> Now you never changed previous during the loop so it is still 0.
> and last is the value from the last line of the file.
> So the if block will only execute if the last value is negative.
> Is that likely?
>
> >     print("split here"+i)
>
> i is never changed in the loop either so i is also zero.
> But i is not previous!
>
> >     previous=last
> >     i=i+1
>
> These lines change the variables but its too late, we are
> at the end of the code.
>
> And worryingly you have not closed the file.
>
> Things to change?
> 1) Use a with statement for opening the file - then you don't
> need to worry about closing it.
>
> 2) Move the if statement and block inside the loop.
> But consider whether you really only want the previous
> and i increment to be done when last < previous?
>
> 3) put more print statements in. Especially put
> in
>
> print("last = ", last)
> print("previous = ", previous)
>
> Just before the if statement.
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
>
> _______________________________________________
> 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