[英]Python output from a specific line in a file
一種方法是使用readlines
。 這個問題已經在Read specific lines only (Python) 中得到了回答,下次嘗試做一些搜索:P
with open(“example.txt”,”r”) as f:
lines = f.readlines() # this will gets all the lines at once
line1 = lines[0] # get the first line
line3 = lines[2] # get the third line
result_int = int(line1) + int(line3) # if you are doing integer addtion. -> 3
result_str = line1 + line3 # will give you: 13
在許多編程語言中,索引從0
而不是1
。 這就是我們使用索引 0 和 2 獲取它的原因。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.