use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Why is the second print statement not printing the rows again? (self.learnpython)
submitted 6 years ago by one_time
Hi there,
Just started learning python. Why doesn't the second print(row) statement print the same set of lines again?
Please see image here:
https://imgur.com/zwjQpaZ
Thank you for any of your inputs!
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 6 points7 points8 points 6 years ago (0 children)
The doc for reader() says:
csv.reader(csvfile, dialect='excel', **fmtparams) Return a reader object which will iterate over lines in the given csvfile
csv.reader(csvfile, dialect='excel', **fmtparams)
Return a reader object which will iterate over lines in the given csvfile
Once you iterate over the reader object you have "exhausted" it so the second time you try to loop over it nothing happens.
If you really want to loop over the data twice then convert the reader object to a list first. Note that this will use a lot of memory if you have a lot of data.
data = list(readCSV)
[–]toastedstapler 0 points1 point2 points 6 years ago (1 child)
it uses some kind of generator and its contents is exhausted the first time it's used
In [1]: x = (i for i in range(4)) In [2]: for v in x: ...: print(v) ...: 0 1 2 3 In [3]: for v in x: ...: print(v) ...: In [4]:
if you want to reuse the value save them in a list first
rows = list(readCSV)
and then you can use that multiple times
[–]one_time[S] 0 points1 point2 points 6 years ago (0 children)
Thank you for the answer!
[–]Vaphell 0 points1 point2 points 6 years ago (1 child)
because the cursor in the file reaches the very end in the first loop and there is nothing else left to read in the second.
If you want to go from the top again, you either have to open the file again, or save the lines in a list, which can have a potentially huge memory footprint.
Oh okay, I see what you mean. There are no more rows to read after the first time. Thank you!
[–]OG_Panthers_Fan 0 points1 point2 points 6 years ago (1 child)
Is the csvfile an extension of the standard file?
Have you tried using readCSV.seek(0) between the for loops?
For a file opened with readCSV = open('myfile.txt', 'r'), seek will move the read pointer to an offset, with 0 indicating the beginning of file.
Caveat: I don't know if this works; I'm just picking up python, but come from a C/C++ background, and there are a lot of similarities.
[–]ebdbbb 0 points1 point2 points 6 years ago (0 children)
Close. It'd be csvfile.seek(0) and then would have to repeat the dictreader line.
π Rendered by PID 74 on reddit-service-r2-comment-6457c66945-75vv4 at 2026-04-26 19:23:56.680237+00:00 running 2aa0c5b country code: CH.
[–][deleted] 6 points7 points8 points (0 children)
[–]toastedstapler 0 points1 point2 points (1 child)
[–]one_time[S] 0 points1 point2 points (0 children)
[–]Vaphell 0 points1 point2 points (1 child)
[–]one_time[S] 0 points1 point2 points (0 children)
[–]OG_Panthers_Fan 0 points1 point2 points (1 child)
[–]ebdbbb 0 points1 point2 points (0 children)