Python Forum
Continuously iterating through a csv file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Continuously iterating through a csv file
#1
hi, I am writing python code that reads values from a csv file, I have the reading down but when the program reaches the end of the csv I need it to start from the top. So it will continuously read the data over and over until I tell it to stop.

here is my current reader code
  
csvname=input("Enter the csv file path : \n")
    with open(csvname, 'rt') as f:
                reader= csv.DictReader(f)
                while True: 
                    while mainFlag==True:
                        start_time = time.monotonic()       
                        for row in reader :                       
                                interger= int(row['setpos'])
                                print(interger)
                                pos= position_setpoint(interger)
                                my_drive.axis0.controller.move_to_pos(pos)
                                actual_time = time.monotonic()
                                t0 = actual_time - start_time
                                time.sleep(adjSpeed)  
I tried using f.seek(0) but it just gave me errors
I appreciate any help
Reply
#2
I believe f.seek(0)
will do the trick. Remember to reset any counters, etc that you might be using.
Reply
#3
You can use fd.seek(0) if there was no next call.
It works with the csv-module:

import csv
import sys
import time


def csv_cycle(file):
    with open(file) as fd:
        reader = csv.DictReader(fd)
        while True:
            for row in reader:
                yield row
            fd.seek(0)
            fd.readline() # skips the header


file = sys.argv[1]

for row in csv_cycle(file):
    # do something with row
    time.sleep(0.5)
    print(row)
When then for-loop is done, then fd.seek(0) is called. The fileobject goes back to position 0 and the csv.DictReader will use use.
Then you have to skip one line, but don't use the next call on the fd. Otherwise the fileobject is locked for seeking to different positions.
During the creation of DictReader, it reads automatically the first line of the csv-file to get the field names.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to use asyncio run_forever to continuously handle requests mansky 1 663 Oct-28-2023, 04:26 AM
Last Post: deanhystad
  How to continuously receive messages until specified to stop using Bleak jacobbreen25 3 2,117 Dec-28-2022, 04:25 PM
Last Post: jacobbreen25
  How to listen to clipboard content change without polling the clipboard continuously? edgelord 0 2,474 Nov-27-2020, 06:07 AM
Last Post: edgelord
  Custom logging handler looping continuously linuxaddikt 0 1,785 Mar-12-2020, 06:58 PM
Last Post: linuxaddikt
  How to Continuously Remove Letters from Words ZQ12 1 2,550 Nov-23-2019, 05:31 PM
Last Post: perfringo
  Working with CSV data and iterating through a file skoobi 1 1,568 Aug-13-2019, 03:28 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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