[Tutor] How to change the values in python code?
samira
samiraeastcoast at gmail.com
Thu Nov 10 14:33:05 EST 2022
Hi all
Thanks for your comments and sorry I did not get a chance to respond back.
I did not wish to stir the pot, LOL just trying to get help. As a newbie it
is not always easy to pass on the clear content.
As David mentioned below "the final number in the line (aka "last") is
increasing, until line 1342 where 493 < 48075 from the previous line" .
Thanks for pointing this out. However, this is just an example. This
happens multiple times throughout the data with different numbers. That's
where I need to find where this happens and get the python print" split
here" so I can split the file at those locations. I have a large data set
and manually finding it is impossible.
In simple language, I would need to write a code that reads line by line
and when the above happens, prints "split here" then actually splits the
file at that location and saves it to a new file. I know how to write a
code to save it to file and all but I just can't get the first part right.
If not possible, it is all good and I will find another solution.
Thanks again
Sam
On Mon, Nov 7, 2022 at 7:20 PM Alan Gauld via Tutor <tutor at python.org>
wrote:
> On 07/11/2022 20:43, samira wrote:
> > 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'm still not clear what exactly you are looking for.
> However, to your data and code...
>
> > 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
>
> It might help if you point those out because I'mnot clear which
> ones you are looking for.
>
> For example:
>
> > Line1: -1.75, 1.08, 10.35, -0.10, -0.01, -0.01, 23.19, 488
>
> We have 8 Fields, indexed 0-7. (based on splitting by a comma)
>
>
> > 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
>
> Here index 1 and 4 are both less than previous.
> Which if any is significant?
>
> > Line5: -1.77, 1.09, 10.40, -0.10, -0.01, -0.01, 23.28, 1000
>
> And here index 1 and 6 are less.
>
> > Line6: -1.72, 1.08, 10.40, -0.10, -0.00, -0.01, 23.31, 1032
>
> Again it is 1 and 4
>
> > 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
>
> And here it is 1, 2, 4 and 7.
>
> Again, what makes this row different to the others?
>
> > previous=0
> >
> > with open ('test-copy.txt', mode="r") as timestamp:
> > for line in timestamp:
> > x=line.split(", ")
>
> Again you loop though all tyhe lines in the file splitting
> them then throwing the data away leaving only the last line
> in x.
>
> > last=int(x[7])
>
> Now you assign index 7, field 8, to last.
> You never change previous so it is always zero.
>
> > print("last = ", last)
> > print("previous = ", previous)
> >
> > if last < previous :
> > print("split here")
>
> Here you compare last to 0. It will only be less if x[7] is negative.
> But the sample data suggests it is always positive.
>
> I suspect you need more code inside the initial for loop,
> something like this(untested)
>
> previous = 0
> with open ('test-copy.txt', mode="r") as timestamp:
> for line in timestamp:
> x=line.split(", ")
> last=int(x[7])
> print("last = ", last)
> print("previous = ", previous)
> if last < previous :
> print("split here - last = ", last)
> previous = last
>
>
> --
> 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