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

python におけるシフト演算子(<< と >> )の使い方

32 = 2^5 なので、 << 1 とすると数値は2倍され、 >> 1 とすると数値が2分の1となる。 例 print(32 << 0) print(32 << …

no image

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

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

no image

matplotlib で、凡例を表示する位置を変更する方法

凡例(legend)の表示位置を変更するには、axis の legend の loc を設定する。 次のリンクhttps://matplotlib.org/stable/api/_as_gen/mat …

no image

python3 で、すべて小文字かどうかを判定する方法

islower() 関数を使う。 例 print(‘abcde’.islower()) print(‘abcDe’.islower()) 結果 True False

no image

matplotlib で、グラフの点の見た目を変更する方法

「marker」で指定すればよい。 次のページを参考に、plotの「marker」を変更する。 https://matplotlib.org/stable/api/markers_api.html 例 …