python3

python でファイルを1行おきに読み込む方法

投稿日:

readline を使って次のように書く。

ファイル:data.txt

# id name age
1 佐藤太郎 10
2 鈴木花子 18
3 坂本明美 21
4 松村光子 24
5 小川奏子 18
6 戸田瑞季 30
7 吉本未央 21
8 大野夏季 32

スクリプト

f = open('data.txt','r')
while True:
    first_line = f.readline()
    second_line = f.readline()
    print(second_line, end='')
    if not second_line: break

結果

1 佐藤太郎 10
3 坂本明美 21
5 小川奏子 18
7 吉本未央 21

-python3
-,

執筆者:


comment

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

関連記事

no image

matplotlib でエラーバーを設定する方法

pyplot.errorbar を使って設定する。 オプションの詳細は https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.erro …

no image

matplotlib で、縦横の線(グリッド)を描く方法

pyplot grid で縦横線(罫線のようなもの)を引くことができる。 例 from matplotlib import pyplot as plt data_x = [25,26,27] …

no image

matplotlib でグラフの中に注釈の文字を書く方法

annotate を使う。 例 import numpy as np from matplotlib import pyplot as plt x = np.linspace(-10,10,100) …

no image

matplotlib でグラフの背景の色を変える方法(facecolor)

次のように、axes で facecolor を変更すればよい。 例 from matplotlib import pyplot as plt x = [2,7,8] y = [7,1 …

no image

matplotlib で、グラフのタイトルに上付き文字を入力する方法

数学記号などで、グラフのタイトルに上付きの文字を入力したい場合がある。 その場合は次のように書いて設定できる。 例 import numpy as np from matplotlib import …