如何打印前10行,并进行30秒的休眠,然后循环直到EOF。

huangapple go评论95阅读模式
英文:

How do I print the 10 lines and take 30 sleep time and so on loop until EOF

问题

  1. f = open('test.csv', 'r')
  2. for line in f:
  3. print(line)
  4. f.close()

我的输出应该像这样:

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 10 秒休眠时间
  12. 11
  13. 12
  14. 13
  15. 14
  16. 15
  17. 16
  18. 17
  19. 18
  20. 19
  21. 20
  22. 直到文件结束
英文:
  1. f = open('test.csv', 'r')
  2. for line in f:
  3. print(line)
  4. f.close()

my output should be like this:

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10

10 sec sleep time

  1. 11
  2. 12
  3. 13
  4. 14
  5. 15
  6. 16
  7. 17
  8. 18
  9. 19
  10. 20

and so on until EOF

答案1

得分: 3

你只需要一个行计数器。 enumerate 可以做到这一点。

  1. f = open('test.csv', 'r')
  2. for n, line in enumerate(f):
  3. print(line)
  4. if n % 10 == 9:
  5. time.sleep(10)
英文:

All you need is a line counter. enumerate can do that.

  1. f = open('test.csv','r')
  2. for n,line in enumerate(f):
  3. print(line)
  4. if n % 10 == 9:
  5. time.sleep(10)

答案2

得分: 1

使用 while 循环和 readlines 方法,以下是代码部分的中文翻译:

  1. with open('test.csv', 'r') as f:
  2. lines = f.readlines()
  3. i = 0
  4. while i < len(lines):
  5. print(' '.join(lines[i:i+10]))
  6. time.sleep(10)
  7. i += 10

如果您需要进一步的翻译或有其他问题,请随时提出。

英文:

This works using the while loop and readlines method.

  1. with open(&#39;test.csv&#39;, &#39;r&#39;) as f:
  2. lines = f.readlines()
  3. i = 0
  4. while i &lt; len(lines):
  5. print(&#39; &#39;.join(lines[i:i+10]))
  6. time.sleep(10)
  7. i += 10

huangapple
  • 本文由 发表于 2023年3月31日 03:23:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75892209.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定