Outputs a line containing the specified character string from a text file

A script that "outputs a line containing the specified character string from a text file".

In my case, Python scripts are often written when processing text files.

First, write this script as a template, and add processing later. \ # Therefore, this script can be written at the "hands move" level (^^;)

It comes in handy when you don't have the grep command or editor function!

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

ld = open("text file.txt")
lines = ld.readlines()
ld.close()

for line in lines:
    if line.find("Search character") >= 0:
        print(line[:-1])

Recommended Posts

Outputs a line containing the specified character string from a text file
Output the line containing the specified string
How to extract the desired character string from a line 4 commands
Download the image from the text file containing the URL
[Ansible] Example of playbook that adds a character string to the first line of the file
# 5 [python3] Extract characters from a character string
[Python] Read the specified line in the file
Extract lines that match the conditions from a text file with python
Use BeautifulSoup to extract a link containing a string from an HTML file
Read line by line from a file with Python
[AWS] Detects the specified character string from the Lambda execution log and notifies slack
Python --Read data from a numeric data file and find the multiple regression line.
Python version (PHP to Python) that deletes the subsequent character string from the specified character string (extension)
# Function that returns the character code of a string
Output a character string with line breaks in PyYAML
[Python] Programming to find the number of a in a character string that repeats a specified number of times.
How to quickly count the frequency of appearance of characters from a character string in Python?
Create a data frame from the acquired boat race text data
A program that removes specific characters from the entered text
Access the file with a relative path from the execution script.
[Addition] A memo for dividing a character string containing multiple spaces
Python> Read from a multi-line string instead of a file> io.StringIO ()
Export a gzip-compressed text file
[Python scraping] Output the URL and title of the site containing a specific keyword to a text file
[Introduction to Python] How to split a character string with the split function
How to get a string from a command line argument in python
Save the Pydrive authentication file in a different directory from the script
Read a file in Python with a relative path from the program
Get the formula in an excel file as a string in Python
[Golang] Check if a specific character string is included in the character string
Is it a character string operation?
Create a pandas Dataframe from a string.
Python-dynamically call a function from a string
Test & Debug Tips: Create a file of the specified size in Python
How to put a line number at the beginning of a CSV file
[Introduction to Python] How to write a character string with the format function
Get the average salary of a job with specified conditions from indeed.com
[Implementation example] Read the file line by line with Cython (Python) from the last line
<Python> A quiz to batch convert file names separated by a specific character string as part of the file name