ファイルを読み込んで、1行目だけをコンソールに出力するには readlines() を使う。
例
f = open("data.txt") lines = f.readlines() print(lines[0][:-1]) f.close()
読み込みファイル
data.txt の中身は
1 2 3 4 5 6 7 8 9
結果
1 2 3
雑記
投稿日:
ファイルを読み込んで、1行目だけをコンソールに出力するには readlines() を使う。
f = open("data.txt") lines = f.readlines() print(lines[0][:-1]) f.close()
data.txt の中身は
1 2 3 4 5 6 7 8 9
1 2 3
執筆者:seyanen
関連記事
文章中でインテグラル(積分記号)を大きくするには、\displaystyle と書けばよい。 例 usual $\int \frac{ax+b}{cx+d} dx$display style $\di …
罫線を出力するには、pyplot.grid を使う。 例 import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 2 …
2023/01/18
matplotlib のグラフ作成と gnuplot との対応 比較
2022/10/14
pythonで配列(リスト)の、ある要素がわかっているときにその次の要素を取得する方法。