英文:
How do I print the 10 lines and take 30 sleep time and so on loop until EOF
问题
f = open('test.csv', 'r')
for line in f:
print(line)
f.close()
我的输出应该像这样:
1
2
3
4
5
6
7
8
9
10
10 秒休眠时间
11
12
13
14
15
16
17
18
19
20
直到文件结束
英文:
f = open('test.csv', 'r')
for line in f:
print(line)
f.close()
my output should be like this:
1
2
3
4
5
6
7
8
9
10
10 sec sleep time
11
12
13
14
15
16
17
18
19
20
and so on until EOF
答案1
得分: 3
你只需要一个行计数器。 enumerate
可以做到这一点。
f = open('test.csv', 'r')
for n, line in enumerate(f):
print(line)
if n % 10 == 9:
time.sleep(10)
英文:
All you need is a line counter. enumerate
can do that.
f = open('test.csv','r')
for n,line in enumerate(f):
print(line)
if n % 10 == 9:
time.sleep(10)
答案2
得分: 1
使用 while 循环和 readlines 方法,以下是代码部分的中文翻译:
with open('test.csv', 'r') as f:
lines = f.readlines()
i = 0
while i < len(lines):
print(' '.join(lines[i:i+10]))
time.sleep(10)
i += 10
如果您需要进一步的翻译或有其他问题,请随时提出。
英文:
This works using the while loop and readlines method.
with open('test.csv', 'r') as f:
lines = f.readlines()
i = 0
while i < len(lines):
print(' '.join(lines[i:i+10]))
time.sleep(10)
i += 10
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论