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

Alan Gauld alan.gauld at yahoo.co.uk
Thu Nov 10 17:20:16 EST 2022


On 10/11/2022 19:33, samira wrote:

> As David mentioned below "the final number in the line (aka "last") is
> increasing, until line 1342 where 493 < 48075 from the previous line" .

OK, That makes life much easier.

> where I need to find where this happens and get the python print" split
> here" so I can split the file at those locations.

You don't really want to split the original file, you just
want to output the data into a different new file.

> If not possible, it is all good and I will find another solution.

It ids very possible, especially now you have told us that
it is only one field we need to monitor.

In fact your code is not far from correct, you just need
to put more code inside the for loop.

Do you understand the significance of indentation in Python?


for item in sequence:
    code to be executed in loop
    more loop code here
code executed after loop.

Only the indented code is repeated, the code aligned with
the for are only executed once after the loop completes.

So in your code you have:

>>> with open ('test-copy.txt', mode="r") as timestamp:
>>>     for line in timestamp:
>>>           x=line.split(", ")

That loops through the entire file splitting every
line and assigning the result to x. Each time a new line
is read it is split and its list of values is assigned
to x, throwing the previous line away.

You need to add your comparison code inside the loop,
but at present you have it outside.

In outline it should look something like:

>> 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

Notice the difference in the indentation level.
This time all of the code after the for... line is
executed for every line in the file.

To do what you want you need to add an output filename variable and
instead of just printing the message write the line to the output file.
Everytime the split is needed just change the output filename and open
the new file (close the old one first!) Something like:

inputfile = open("WhateverItsCalled")
outroot = 'Part-'
count = 1
outfile = outroot + str(count)+".dat"
output = open(outfile,'w')
previous = 0

for line in inputfile:
    fields = line.split(', ')
    if fields[-1] < previous:
       output.close()
       count +=1
       outfile = outroot+str(count)+".dat"
       output = open(outfile, 'w')
    output.write(line + '\n')  # add a newline
    previous = fields[-1]
inputfile.close()
output.close()

We can tighten that up quite a lot but I've kept it
as self explanatory as possible.

That should take your original file and produce a set
of files called Part-1.dat, Part-2.dat, ....Part-N.dat.
You will likely need to tweak it, I haven't run or
tested it in any way.

-- 
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





More information about the Tutor mailing list