[英]Outputting a specific line from a list in a file and converting it into a string
I am stuck on a task of where I have to output n-th line from a file and I need it outputted as a string.我被困在一个任务上,我必须从文件中找到 output 第 n 行,我需要将它作为字符串输出。
file = open("/usercode/files/pull_ups.txt")
n = int(input())
print(file.readlines(n))
file.close()`enter code here
This outputs '['Day 0, 8 pull ups\n']' and I need it to output Day 1, 8 pull ups这输出'['Day 0, 8 pull ups\n']' 我需要它到 output Day 1, 8 pull ups
with open("file.txt") as file: # cleaner way of opening a file
n = int(input())
for i in range(n-1): # skip first n-1 lines of a file
file.readline()
print(file.readline()) # read n-th line
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.