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



我是python的新手,我尝试了许多CSV读取函数来打印一列CSV文件中的一系列行(没有标题(,每次打印都会有一些延迟,但没有成功,我想把它放进一个函数中,但现在,我试着先让打印工作,然后我会尝试使用函数,这里是我尝试的:

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

我想在";some_id=";来自CSV文件,但不工作,

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)

编辑:出于学习的目的,我想学习另一种打印CSV文件行的方法,中间有延迟,所以我尝试了Cyrex的以下代码,但没有成功,我使用visual studio代码运行python,以及的部分:

for row in list(csv_reader)

错误显示在关闭伪装上:对于列表中的行(csv_rereader(^SyntaxError:语法无效

代码::

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

如果您想打印所有行,然后等待5秒,请尝试:

>>> 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)

如果您想打印一行,请等待5秒,然后继续打印并等待,请尝试:

>>> 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)

它应该是类似的smthg

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) 

相关内容

  • 没有找到相关文章

最新更新