[英]Python regular expression finding a word
I have this question I hope you could help me.. 我有这个问题,希望您能帮助我。
Basically I have an app where I open a file, read the word from my file and print that word by console. 基本上,我有一个应用程序可以打开文件,从文件中读取单词,然后通过控制台打印该单词。 But I want to print "word no found" in case of no finding the word.
但是我想打印“找不到单词”,以防找不到单词。
Here's my python : 这是我的python:
import re
file = open("example.txt","r")
content = file.read()
file.close()
car = re.findall('car\s(.*?)\s',open('example.txt','r').read())
toy = re.findall('toy\s(.*?)\s',open('example.txt','r').read())
print car[0]
print toy[0]
here's my text file: 这是我的文本文件:
car red
车红
toy green
玩具绿
I'm getting by console this: 我通过控制台得到这个:
red
green
Working great as you can see but if I don't have the word "toy" in my file. 如您所见,效果很好,但是如果我的文件中没有“玩具”一词。 There's a possible way to get something like this by console :
有一种可能的方式可以通过控制台获得类似的信息:
Red
No toy found!
Thank you! 谢谢!
Do not
open the file
in several locations. Do not
在多个位置打开file
。 It is a bad practice. 这是一个坏习惯。 Open in one place and use that
object
. 在一个地方打开并使用该
object
。
content = open('example.txt', 'r').read()
if 'car' in content:
print("found")
else:
print("not found")
import re
file = open("example.txt","r")
content = file.read()
file.close()
car = re.findall('car\s(.*?)\s',open('example.txt','r').read())
toy = re.findall('toy\s(.*?)\s',open('example.txt','r').read())
if car == []:
print "No car found"
else:
print car[0]
if toy == []:
print "No toy found"
else:
print toy[0]
Instead of printing toy[0]
immediately, use an if-else
statement to check if there is a toy before printing to the console. 不要立即打印
toy[0]
,而应使用if-else
语句在打印到控制台之前检查是否有玩具。 If there is no toy, print "No toy found". 如果没有玩具,请打印“找不到玩具”。
Based on the code sample you supplied, the solution would look like this: 根据您提供的代码示例,解决方案如下所示:
import re
file = open("example.txt","r")
content = file.read()
file.close()
car = re.findall('car\s(.*?)\s',open('example.txt','r').read())
toy = re.findall('toy\s(.*?)\s',open('example.txt','r').read())
if toy:
print toy[0]
else:
print 'No toy found'
if car:
print car[0]
else:
print 'No car found'
Note that while this solution may work, there are some improvements that can be made to your code overall. 请注意,尽管此解决方案可能有效,但总体上可以对代码进行一些改进。 The improvements include:
改进包括:
with
statement to easily close the file you opened with
语句轻松关闭打开的文件 find
as it is a keyword in Python and may lead to unexpected behavior later in your application find
的变量,因为它是Python中的关键字,并可能在以后的应用程序中导致意外行为 content
variable for the regular expression match (so that you don't have to read from the file again). content
变量中的数据进行正则表达式匹配(这样就不必再次从文件中读取数据)。 The improvements will leave you with code that looks like this: import re 这些改进将使您获得如下代码:import re
#use with statement to open and close file cleanly
with open('example.txt','r') as f:
# save file contents in the content variable so you don't need to open the file again
content = f.read()
# use data stored in content variable for your regular expression match
car = re.findall('car\s(.*?)\s',content)
toy = re.findall('toy\s(.*?)\s',content)
if toy:
print toy[0]
else:
print 'No toy found'
if car:
print car[0]
else:
print 'No car found'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.