How to search for a specific character in a file using Python?

1 min read
python programming

In this article, a simple python script is provided that can be used to search for a specific character in a file.

If you have a file consisting of some information including name, address, email, post, and so on. You want to extract only email addresses present in that file, then use the following script/block of code. Here, this input file is named “employee_info.txt”.

#!/usr/bin/env python


value = ''
str = 'Email ID:'

file = open('employee_info.txt')

result = ''
for line in file:
			if '@' in line:				#looking for specific character
				value = line.splitlines()
				print ("Email ID:", value)
file.close()				

Save this script with .py extension. Suppose, save it as ‘script.py’. Open a terminal on Ubuntu or command prompt on Windows and type:

$ python script.py

If you want to redirect the output to a file, then type:

$ python script.py > output.txt
Muniba is a Bioinformatician based in the South China University of Technology. She has cutting edge knowledge of bioinformatics tools, algorithms, and drug designing. When she is not reading she is found enjoying with the family. Know more about Muniba

Leave a Reply

Installing pyrx on ubuntu
Previous Story

Installing Pyrx on Ubuntu

smORFunction: A tool for ORfs and microproteins function prediction
Next Story

smORFunction: A Tool for ORFs and Microproteins Function Prediction

Latest from Bioinformatics Programming

0 $0.00