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.
Pedroski55 and rob101 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
  PyYAML read list of int zisco 2 272 Apr-02-2024, 12:36 PM
Last Post: zisco
  append str to list in dataclass flash77 6 451 Mar-14-2024, 06:26 PM
Last Post: flash77
  Recommended way to read/create PDF file? Winfried 3 2,857 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,409 Nov-09-2023, 10:56 AM
Last Post: mg24
  How to read module/class from list of strings? popular_dog 1 467 Oct-04-2023, 03:08 PM
Last Post: deanhystad
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,154 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Program to find Mode of a list PythonBoy 6 1,062 Sep-12-2023, 09:31 AM
Last Post: PythonBoy
  FileNotFoundError: [WinError 2] The system cannot find the file specified NewBiee 2 1,551 Jul-31-2023, 11:42 AM
Last Post: deanhystad
  read file txt on my pc to telegram bot api Tupa 0 1,095 Jul-06-2023, 01:52 AM
Last Post: Tupa
  parse/read from file seperated by dots giovanne 5 1,088 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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