Python поиск подстроки файле

Поиск подстроки в файле

Доброго времени суток!
Прошу у Вас помощи по следующей проблеме:
Имеется текстовый файл, который выглядит вот так:

Иванов;Иван;Иванович;14;Москва
Иванов;Петр;Иванович;13;Подольск
Петров;Сергей;Валентинович;15;Москва
и т.д.

ЯП Python
Необходимо, чтобы программа получала от пользователя критерии и искала им соответствие.
То есть пользователь вводит первую команду «Иванов». Программа видит, что Ивановых два и просит далее указать имя. Так она спрашивает каждое поле, пока нужный нам Иванов не останется в единственном экземпляре. Его-то нам и нужно вывести, а если Нужный нам Иванов не найден, то вернуть к предыдущему параметру, где еще был шанс выбрать нужного Иванова
Наверное, нужно считать строки и сделать из каждой несколько объектов класса, но недостаток знания ЯП в целом и Python в частности мне этого сделать не позволяет.
Не кидайтесь, пожалуйста, тапками, если где-то криво описал. Заранее спасибо!

Поиск подстроки
Уважаемые форумчане, кодеры, кто может написать мне код на этом примере; Поиск подстрок. Вызов.

Поиск подстроки
Напишите программу, которая находит кота. Пользователь вводит сначала количество строк, потом сами.

Читайте также:  Html input text index

Поиск подстроки
здравствуйте, у меня есть в строке текст: edmnchislo;1данныедействие;2данныедействий;\n.

Поиск подстроки в файле
Здравствуйте! Подскажите как найти подстроку в файле, в нем куча строк и надо найти только кусок.

Эксперт Python

Лучший ответ

Сообщение было отмечено diazboris как решение

Решение

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#!/usr/bin/env python3 t = '''\ Иванов;Иван;Иванович;14;Москва Иванов;Петр;Иванович;13;Подольск Петров;Сергей;Валентинович;15;Москва''' ls = [i.split(';') for i in t.split('\n')] print(ls) line = input('> ') for f, i, o, old, city in ls: if line in f: print(f, i, o, old, city)
with open('file.txt') as f: t = f.read()

Источник

Python Search for a String in Text Files

In this Python tutorial, you’ll learn to search a string in a text file. Also, we’ll see how to search a string in a file and print its line and line number.

After reading this article, you’ll learn the following cases.

  • If a file is small, read it into a string and use the find() method to check if a string or word is present in a file. (easier and faster than reading and checking line per line)
  • If a file is large, use the mmap to search a string in a file. We don’t need to read the whole file in memory, which will make our solution memory efficient.
  • Search a string in multiple files
  • Search file for a list of strings

We will see each solution one by one.

Table of contents

How to Search for a String in Text File

Use the file read() method and string class find() method to search for a string in a text file. Here are the steps.

  1. Open file in a read modeOpen a file by setting a file path and access mode to the open() function. The access mode specifies the operation you wanted to perform on the file, such as reading or writing. For example, r is for reading. fp= open(r’file_path’, ‘r’)
  2. Read content from a file Once opened, read all content of a file using the read() method. The read() method returns the entire file content in string format.
  3. Search for a string in a file Use the find() method of a str class to check the given string or word present in the result returned by the read() method. The find() method. The find() method will return -1 if the given text is not present in a file
  4. Print line and line number If you need line and line numbers, use the readlines( ) method instead of read() method. Use the for loop and readlines() method to iterate each line from a file. Next, In each iteration of a loop, use the if condition to check if a string is present in a current line and print the current line and line number

Example to search for a string in text file

I have a ‘sales.txt’ file that contains monthly sales data of items. I want the sales data of a specific item. Let’s see how to search particular item data in a sales file.

sales text file

def search_str(file_path, word): with open(file_path, 'r') as file: # read all content of a file content = file.read() # check if string present in a file if word in content: print('string exist in a file') else: print('string does not exist in a file') search_str(r'E:\demos\files_demos\account\sales.txt', 'laptop')

Search file for a string and Print its line and line number

Use the following steps if you are searching a particular text or a word in a file, and you want to print a line number and line in which it is present.

  • Open a file in a read mode.
  • Next, use the readlines() method to get all lines from a file in the form of a list object.
  • Next, use a loop to iterate each line from a file.
  • Next, In each iteration of a loop, use the if condition to check if a string is present in a current line and print the current line and line number.

Example: In this example, we’ll search the string ‘laptop’ in a file, print its line along with the line number.

# string to search in file word = 'laptop' with open(r'E:\demos\files_demos\account\sales.txt', 'r') as fp: # read all lines in a list lines = fp.readlines() for line in lines: # check if string present on a current line if line.find(word) != -1: print(word, 'string exists in file') print('Line Number:', lines.index(line)) print('Line:', line)
laptop string exists in a file line: laptop 10 15000 line number: 1

Note: You can also use the readline() method instead of readlines() to read a file line by line, stop when you’ve gotten to the lines you want. Using this technique, we don’t need to read the entire file.

Efficient way to search string in a large text file

All above way read the entire file in memory. If the file is large, reading the whole file in memory is not ideal.

In this section, we’ll see the fastest and most memory-efficient way to search a string in a large text file.

  • Open a file in read mode
  • Use for loop with enumerate() function to get a line and its number. The enumerate() function adds a counter to an iterable and returns it in enumerate object. Pass the file pointer returned by the open() function to the enumerate() .
  • We can use this enumerate object with a for loop to access the each line and line number.

Note: The enumerate(file_pointer) doesn’t load the entire file in memory, so this is an efficient solution.

with open(r"E:\demos\files_demos\account\sales.txt", 'r') as fp: for l_no, line in enumerate(fp): # search string if 'laptop' in line: print('string found in a file') print('Line Number:', l_no) print('Line:', line) # don't look for next lines break
string found in a file Line Number: 1 Line: laptop 10 15000

mmap to search for a string in text file

In this section, we’ll see the fastest and most memory-efficient way to search a string in a large text file.

Also, you can use the mmap module to find a string in a huge file. The mmap.mmap() method creates a bytearray object that checks the underlying file instead of reading the whole file in memory.

import mmap with open(r'E:\demos\files_demos\account\sales.txt', 'rb', 0) as file: s = mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) if s.find(b'laptop') != -1: print('string exist in a file')

Search string in multiple files

Sometimes you want to search a string in multiple files present in a directory. Use the below steps to search a text in all files of a directory.

  • List all files of a directory
  • Read each file one by one
  • Next, search for a word in the given file. If found, stop reading the files.
import os dir_path = r'E:\demos\files_demos\account' # iterate each file in a directory for file in os.listdir(dir_path): cur_path = os.path.join(dir_path, file) # check if it is a file if os.path.isfile(cur_path): with open(cur_path, 'r') as file: # read all content of a file and search string if 'laptop' in file.read(): print('string found') break

Search file for a list of strings

Sometimes you want to search a file for multiple strings. The below example shows how to search a text file for any words in a list.

words = ['laptop', 'phone'] with open(r'E:\demos\files_demos\account\sales.txt', 'r') as f: content = f.read() # Iterate list to find each word for word in words: if word in content: print('string exist in a file')

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

About Vishal

I’m Vishal Hule, Founder of PYnative.com. I am a Python developer, and I love to write articles to help students, developers, and learners. Follow me on Twitter

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 10 questions
  • Each Quiz contains 12-15 MCQ

Источник

Найти строку в файле по слову и скопировать строку целиком

Найти в текстовом файле строку по заданному слову и заменить ей определённую часть строки в другом файле
Всех с Новым Годом! Наверное избитая тема, но все же. Задача такая: Существует два файла.

найти данные по определенному слову в столбце и скопировать всю строку в новую стр
Добрый день, у меня такой вопрос: во вложении есть файл, в нем базовые данные абонентов (фио.

Найти позицию в файле по ключевому слову и добавить строку после ключевого слова
Добрый день дамы и господа! Нужна помощь. Есть файл .bin в котором записан перечень путей к файлам.

Как удалить строку в файле и как вывести целую строку по слову, а не одно лишь слово
Создаю файловую базу данных, решил реализовать удаление целой строки и поиск строки по одному.

Лучший ответ

Сообщение было отмечено dexterdx как решение

Решение

#!/usr/bin/python3 word = 'диван' # Искомое слово inp = open('11').readlines() for i in iter(inp): if word in i: print(i)

Источник

Оцените статью