I wrote in Python that the natural number N should be received by a means such as a command line argument, and only the first N line of input should be displayed, but for the time being, I could not deny the feeling.Could you write a little more Python-like?
import sys
i = 0
with open("hightemp.txt", "r") asf:
where_str=f.readlines()
while(i!=int(sys.argv[1])):
print(whole_str[i])
i+=1
If there is no problem with readability, you don't have to worry about the details, but you can also write as follows:
import sys
with open("hightemp.txt") as f:
for i in range(int(sys.argv[1])):
print(f.readline())
import sys
from itertools import islice
n=int(sys.argv[1])
with open("hightemp.txt") as f:
for line in islice(f,n):
print(line.rstrip('\n')
© 2024 OneMinuteCode. All rights reserved.