LoginSignup
61
64

More than 3 years have passed since last update.

テキストファイルから指定した文字列を含む行を出力する

Last updated at Posted at 2013-12-01

「テキストファイルから指定した文字列を含む行を出力する」スクリプトです。

私の場合、Pythonスクリプトはテキストファイルを加工する時によく記述します。

まず、ひな形としてこのスクリプトで書いて、後から処理を追加して行きます。
# そのため、このスクリプトは「手が勝手に動く」レベルで記述できます(^^;)

grepコマンドやエディター機能がない時などに、意外と重宝しますよ!

# -*- coding: utf-8 -*-

ld = open("テキストファイル.txt")
lines = ld.readlines()
ld.close()

for line in lines:
    if line.find("検索文字") >= 0:
        print(line[:-1])
61
64
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
61
64