Python Forum
read a text file, find all integers, append to list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
read a text file, find all integers, append to list
#11
What I mean is, certain, "Python objects", for instance the result of csv.reader() can only be used 1 time. I just wonder why.

Example:

import csv
myfile = '/home/pedro/myPython/csv/time.csv'
csv_reader = csv.reader(myfile, delimiter=',')
for row in csv_reader:
    print(row)
Output:
['time', ' comment'] ['2022-04-25 7:06 AM', ' bla '] ['2022-04-25 7:07 AM', ' blabla'] ['2022-04-25 7:08 AM', ' ha'] ['2022-04-25 7:09 AM', ' haha']
But, if you try to use csv_reader again, it's gone!

for row in csv_reader:
    print(row)
You get nothing, csv_reader is "burned" and unavailable. You would have to read it in again.

That was the same with your code in this thread. I didn't put *Nset in the text file, so the whole of file was searched.

After that file was unavailable for the second loop.

I just wonder why that is.

For practical purposes, I read csv_reader into a list, which remains available for as many uses as I want.
Reply
#12
(Aug-11-2022, 12:27 AM)Pedroski55 Wrote: But, if you try to use csv_reader again, it's gone!

That's the way file objects work. Unless you do special handling, the read pointer moves forward in file as you consume it and stops at the end. If you keep reading, it doesn't magically loop back to the front of the file. You're expected to either reopen the file or seek to the front of the file if you want to read it again.

import csv

f = open("time.csv")
r = csv.reader(f)
for line in r:
    print(line)

# lets re-read the file.
f.seek(0)
for line in r:
    print(line)
But reading a file (I/O) is expensive. Better to avoid repeating it. Either try to do everything in one pass, or as you're already doing (if the file is small enough), read it into memory and operate on the copy in memory.
rob101 and Pedroski55 like this post
Reply
#13
Aha, thanks, I have heard of f.seek() but never had cause to use it.

I'll read some more about it!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to read a file as binary or hex "string" so that I can do regex search? tatahuft 3 1,012 Dec-19-2024, 11:57 AM
Last Post: snippsat
  Get an FFMpeg pass to subprocess.PIPE to treat list as text file? haihal 2 993 Nov-21-2024, 11:48 PM
Last Post: haihal
  Read TXT file in Pandas and save to Parquet zinho 2 1,210 Sep-15-2024, 06:14 PM
Last Post: zinho
  Pycharm can't read file Genericgamemaker 5 1,545 Jul-24-2024, 08:10 PM
Last Post: deanhystad
  Python is unable to read file Genericgamemaker 13 3,591 Jul-19-2024, 06:42 PM
Last Post: snippsat
  Connecting to Remote Server to read contents of a file ChaitanyaSharma 1 3,232 May-03-2024, 07:23 AM
Last Post: Pedroski55
  PyYAML read list of int zisco 2 1,617 Apr-02-2024, 12:36 PM
Last Post: zisco
  append str to list in dataclass flash77 6 3,293 Mar-14-2024, 06:26 PM
Last Post: flash77
  Recommended way to read/create PDF file? Winfried 3 4,628 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 3,756 Nov-09-2023, 10:56 AM
Last Post: mg24

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020