[Tutor] Python text file read/compare
Cameron Simpson
cs at cskk.id.au
Thu Sep 29 17:43:54 EDT 2022
On 29Sep2022 15:09, samira khoda <samiraeastcoast at gmail.com> wrote:
>Below is the snapshot of the data. Basically I need to find where the
>timestamps jump down to zero or the lowest number so I can split the file.
>For your information the timestamps are in milliseconds on the last column.
>
>[image: image.png]
This list strips nontext attachments. Please just paste a few lines of
example data directly into your message.
>*And below is the code I wrote but nothing came out of it.
I'll make a few remarks about the code inline below.
>with open('hfx-test-.txt') as kyk:
> op=''
> start=0
> count=1
> for x in kyk.read().split("\n"):
This reads all the data and then breaks it up on newlines. For big files
that can be expensive. Text files are iterables, yielding lines, so you
can write this:
for line in kyk:
x = line.rstrip() # this removes the newline
> if(x=='0'):
You don't need brackets in Python if-statements:
if x =='0':
> if (start==1):
If you don't get any files, presumably `start` is never equal to `1`.
> with open(str(count)+ '.txt', 'w') as opf:
> opf.write(op)
> opf.close()
The `with open()` form does the `close()` for you, you do not need the
`opf.close()`.
> op=''
> count+=1
and I'd have these lines outside the `with` i.e. less indented.
> else:
> start=1
>
> else:
> if(op==''):
> op = x
> else:
> op= op+ '\n' + x
It looks like you're accumulating the `x` values as one big string.
That will work, but it would be more common to accumulate a list< eg:
Up the top: ops = [] # an empty list.
Down here: ops.append(x)
In the `opf` with-statement:
for op in ops:
print(op, file=opf)
ops = [] # we have writtne them, reset the list to empty
>kyk.close()
As with `opf`, the with statement closes the file for you. You don't
need this line.
Finally, if your code's not doing what you intend, then _either_ the
if-statements have the wrong tests _or_ the variables you're testing do
not have the values you expect.
Put some `print()` calls in the code to see what's going on. Examples:
for line in kyk:
x = line.rstrip() # this removes the newline
print("x =", repr(x))
print("start =", start, "count =", count, "op = ", repr(op))
That should show you the lines of data as you read them, and the values
of the variable you're testing. Hopefully that should show you when
things go wrong, and lead you to a fix.
The `repr(x)` expression prints a representation of the value, which is
particularly handy for things like strings.
Cheers,
Cameron Simpson <cs at cskk.id.au>
More information about the Tutor
mailing list