Python,有延迟的行的打印范围

Python, printing range of row with delays

我是 python 的新手,我尝试了很多 CSV 读取函数来打印来自 one-column CSV 文件(没有 header)的一系列行,每次打印都有一些延迟,没有成功,我想把它放到一个函数中,但现在,我先尝试让打印工作,然后我会尝试使用函数,这是我尝试过的:

  from time import sleep
  from csv import reader
  import csv
  import pandas as pd

  df = pd.read_csv('test3.csv')
  print(df.iloc[25:36]) # it prints row 25 till row 36

我用定时器试过了:

  df = pd.read_csv('test3.csv')
  for s in (df.iloc[25:36]):
  print(s) ; sleep(5)

但它只打印第一行,休眠 5 秒,但不打印其余行。 感谢您的帮助

已编辑:

好的,Nikolaos 的新计时器工作得很好, 这是一个简单的函数,我想让它与来自 csv 文件的数据一起工作:

def func1(some_id='', some_Num ='12345'):
return something.func1(some_id=some_id, some_Num = some_Num)
func1() # this works when run

我想从 CSV 文件中在“some_id=”前面填充数据,但不起作用,

def func1(some_id='', some_Num ='12345'):
return something.func1(some_id=some_id, some_Num = some_Num)

with open('test3.csv') as cso:
for s in islice(cso, 25, 37): # from 25 to 36 included, update as needed
    func1(s, end='')
    time.sleep(5)

Edited: for learning purpose I want to learn another method of printing rows of CSV file with delay in between, so I tried the following code from Cyrex, but not working, I used visual studio code to run python, and the part of:

for row in list(csv_reader)

关闭伪装时显示错误: 对于列表中的行(csv_reader) ^ 语法错误:语法无效

代码::

 import json
 import random
 import time
 import os
 from time import sleep
 from csv import reader
 from itertools import islice
 import csv
 import pandas as pd

 with open('test4.csv', 'r') as read_obj:
 csv_reader = reader(read_obj)
 header = next(csv_reader)
 if header != None:  
    list = [] 
    for row in list(csv_reader)
       list.append[row]
    print(list[24:35])
    time.sleep(5)

这是 CSV 文件中的数据:

432323422016048516
3245968643672398
994751490493134145
1245290296886842
2286337388046077
5576337391667210
670175046204272300

应该是这样的

from csv import reader
import time


with open('file.csv', 'r') as read_obj:
    csv_reader = reader(read_obj)
    header = next(csv_reader)

    # Check file if empty

    if header != None:
        
        lst = [] 

        for row in csv_reader:

            
            lst.append[row]

        print(lst[24:35]) # changed 25 to 24 and 36 to 35 coz this iteration is done through lst list not through csv_reader

        time.sleep(5) 

如果你想打印所有行然后等待5s,试试:

>>> from itertools import islice
>>> from csv import reader
>>> import time
>>>
>>> with open('test3.csv') as cso:
...     for s in islice(cso, 25, 37): # from 25 to 36 included, update as needed
...         print(s, end='')
...     time.sleep(5)

如果要打印一行,等待5s,继续打印等待,尝试:

>>> from itertools import islice
>>> from csv import reader
>>> import time
>>>
>>> with open('test3.csv') as cso:
...     for s in islice(cso, 25, 37): # from 25 to 36 included, update as needed
...         print(s, end='')
...         time.sleep(5)