How do I match a word in a text file using python?

2024/9/25 23:15:48

I want to search and match a particular word in a text file.

with open('wordlist.txt', 'r') as searchfile:for line in searchfile:if word in line:print line

This code returns even the words that contain substrings of the target word. For example if the word is "there" then the search returns "there", "therefore", "thereby", etc.

I want the code to return only the lines which contain "there". Period.

Answer
import refile = open('wordlist.txt', 'r')for line in file.readlines():if re.search('^there$', line, re.I):print line

The re.search function scans the string line and returns true if it finds the regular expression defined in the first parameter, ignoring case with re.I. The ^ character means 'beginning of the line' while the $ character means 'end of the line'. Therefore, the search function will only return true if it matches there preceded by the beginning of the line, and followed by the end of the line, aka isolated on its own.

https://en.xdnf.cn/q/71524.html

Related Q&A

Unable to Delete Videos with the Youtube Data API

Cant get deleting videos to work using the Youtube Data API. Im using the Python Client Library.All of this seems straight from the docs, so Im really confused as to why its not working. Heres my fun…

LLDB Python scripting in Xcode

Ive just discovered this handy feature of LLDB that allows me to write Python scripts that have access to variables in the frame when Im on a breakpoint in LLDB. However Im having a few issues when usi…

What technologies exist to create stand alone executables for Python 3?

Other than cx_Freeze, are there any other current maintained tool suites to generate stand alone executables for Python 3k?Are there any other techniques for minimizing preinstallation requirements un…

running multiple threads in python, simultaneously - is it possible?

Im writing a little crawler that should fetch a URL multiple times, I want all of the threads to run at the same time (simultaneously).Ive written a little piece of code that should do that.import thre…

Drawing bounding rectangles around multiple objects in binary image in python

I am trying to write some easy code in python to produce bounding rectangles around objects in a binary image, where there may be 1 or more objects. This is fairly easy to achieve with cv2.boundingRec…

Replicating YEARFRAC() function from Excel in Python

So I am using python in order to automate some repetitive tasks I must do in excel. One of the calculations I need to do requires the use of yearfrac(). Has this been replicated in python?I found this…

creating a pandas dataframe from a database query that uses bind variables

Im working with an Oracle database. I can do this much:import pandas as pdimport pandas.io.sql as psqlimport cx_Oracle as odbconn = odb.connect(_user +/+ _pass +@+ _dbenv)sqlStr = "SELECT * FROM c…

Is there a docstring autocompletion tool for jupyter notebook?

I am looking for a tool/extension that helps you writing python docstrings in jupyter notebook. I normally use VS code where you have the autodocstring extension that automatically generates templates …

Long to wide data. Pandas

Im trying to take my dataframe from a long format in which I have a column with a categorical variable, into a wide format in which each category has its own price column. Currently, my data looks like…

How to wrap text in Django admin(set column width)

I have a model Itemclass Item(models.Model):id = models.IntegerField(primary_key=True)title = models.CharField(max_length=140, blank=True)description = models.TextField(blank=True)price = models.Decima…