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
  [SOLVED] [Beautifulsoup] Find if element exists, and edit/append? Winfried 2 276 Sep-03-2022, 10:14 PM
Last Post: Winfried
  How to modify python script to append data on file using sql server 2019? ahmedbarbary 1 310 Aug-03-2022, 06:03 AM
Last Post: Pedroski55
  Read xml column inside csv file with Python estertabita 2 406 Jul-26-2022, 06:09 PM
Last Post: Larz60+
  what will be the best way to find data in txt file? korenron 2 284 Jul-25-2022, 10:03 AM
Last Post: korenron
  find some word in text list file and a bit change to them RolanRoll 3 422 Jun-27-2022, 01:36 AM
Last Post: RolanRoll
  Using .append() with list vs dataframe Mark17 7 704 Jun-12-2022, 06:54 PM
Last Post: Mark17
  How to find the second lowest element in the list? Anonymous 3 451 May-31-2022, 01:58 PM
Last Post: Larz60+
  TypeError: list indices must be integers or slices, not range Anldra12 2 717 Apr-22-2022, 10:56 AM
Last Post: Anldra12
  Modify values in XML file by data from text file (without parsing) Paqqno 2 544 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Find and delete above a certain line in text file cubangt 12 1,330 Mar-18-2022, 07:49 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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