我想检查包含该号码的文件中的句子是否包含我要打印的句子。
我是python的初学者,我需要一些帮助。
提前致谢
答案 0 :(得分:0)
我真的不明白这个问题,因为你让我感到困惑,无论是单句还是多句,还有多少你想要打印,以及它是否是一个特定的数字。但是,我几乎无法理解它,这就是我从中得到的(无论如何都要寻找如何提出好的问题)
我想您想知道如何在句子或句子列表中找到某些内容并打印出来。这是这样做的:
string = "Whatever string you want this to be, blah blah blah, etc.
for b in string: #This will look for every character in the string making b that character
if b == 'b': #If the character is 'b'
print b #It will print the character
#If not, it will move on to the next character
这就是你如何在字符串中找到一个单词。如果你想要多个字符串,只需将字符串作为一个列表:
strings = ['one string', 'two string', 'three string', 'etc']
for b in strings: #This will look for every string in the strings making b that string
for c in b: #This will look for every character in the b making c that character
if c == 's': #If that character is 's'
print b #It will print the character
#If not, it will move on to the next character
答案 1 :(得分:0)
我使用x
作为您文件的占位符。您可能希望将其替换为myfile.readlines()
或用于循环文件中每一行的任何内容。
x = ['My placeholder file' , 'that contains sentences', 'with and without numbers', 'There should be 2 sentences with numbers.' , 'There should be 3 setences without numbers']
for eachLine in x:
if any(char.isdigit() for char in eachLine):
print eachLine