Python Forum
How to Loop CSV File Beginning at Specific Row?
Thread Rating:
  • 3 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to Loop CSV File Beginning at Specific Row?
#1


I'm working on a project where I have a CSV file containing about 25,000 rows of a unique URL in the first (and only) column throughout the CSV file. I'm iterating through each row, getting the unique URL, doing some processing on some data contained behind the URL once I open each unique URL, and writing some extended data to a 2nd CSV file.

The problem I'm having is that every now and then, something causes my Python script to fail and I've got to restart it and manually edit the initial CSV file of URLs to remove the rows containing URLs I've already processed so that the script resumes with what's the new first line containing the beginning of the next many URLs that I have yet to process.

In my script, I'm writing to another small CSV file the # of the row that was just successfully processed to where I have this as a reference if/when the script fails. For example, my script runs, the last row successfully processed is row 500, and the number 500 is written to my other CSV file.

I'd like to be able to retrieve that # 500 (I know how to do this) when I have to restart the script and have that utilized by the script to know that in the large file of 25,000 rows of unique URLs, I can effectively skip rows 0-500 since I've already processed those, and resume the processing of ongoing unique URLs beginning with row 501.

What I'm trying not to have to do is for the script to have to iterate through each individual row until it sees that I'm at row 500, as that seems to be a time waster. Is there a way after I read the CSV file that contains "500" to have my script open the CSV file of URLs & just "skip" immediately to row 501 and begin iteration from that point forward instead of having to cycle through all rows until it gets to the desired row (501) to begin?

I've read that I could possibly use "islice" to help do this more efficiently, but I haven't seen anything out there that's concrete on the best & most efficient way to accomplish this.

Thanks in advance for any suggestions you can offer.
Reply
#2
With open('my_data', 'r') as data_csv:
    data = csv.reader(data_csv)
    
    for line in data[501:]
        print(line)
Did you try something like this?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
Tells me an error of "csv.reader object is not subscriptable"
Reply
#4
Hm!

with open('my_data', 'r') as data_csv:
    data = csv.reader(data_csv)
    for _ in range(500):  # skip the first 500 rows
        next(data)

    for row in data:
        print(line)
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
Use islice from itertools.
https://docs.python.org/3.6/library/iter...ols.islice

Example:

from itertools import islice

with open('my_data.csv') as fd:
    for row in islice(csv.reader(fd), 500, None):
        print(row)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#6
The isslice recommendation worked PERFECTLY! Thanks so very much! Really appreciate the help!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Just beginning dalekeel 2 187 Apr-15-2024, 11:49 AM
Last Post: Pedroski55
  Extracting specific file from an archive tester_V 4 501 Jan-29-2024, 06:41 PM
Last Post: tester_V
  Program doesnt return beginning bilisim19 2 928 Feb-15-2023, 06:23 PM
Last Post: Larz60+
  Reading Specific Rows In a CSV File finndude 3 981 Dec-13-2022, 03:19 PM
Last Post: finndude
  How to extract specific data from .SRC (note pad file) Shinny_Shin 2 1,263 Jul-27-2022, 12:31 PM
Last Post: Larz60+
  Extracting Specific Lines from text file based on content. jokerfmj 8 2,953 Mar-28-2022, 03:38 PM
Last Post: snippsat
  How to save specific variable in for loop in to the database? ilknurg 1 1,143 Mar-09-2022, 10:32 PM
Last Post: cubangt
  List of dataframe values beginning with x,y or z glidecode 3 1,930 Nov-08-2021, 10:16 PM
Last Post: glidecode
  [Solved] Trying to read specific lines from a file Laplace12 7 3,528 Jun-21-2021, 11:15 AM
Last Post: Laplace12
  Extract specific sentences from text file Bubly 3 3,401 May-31-2021, 06:55 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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