File search and exact match and print search?

I searched, but I could not find any post to help me fix this problem, I found a similar one, but I still could not find a solution to this problem.

Here's the problem I have, I am trying to find a python script to search for a text file, the text file has numbers in the list, and each number corresponds to a line of text, and if raw_input matches the exact number in the text file prints the entire line of text. while it prints any line containing the number.

An example of a problem, user types 20 , then the output is all that contains 2 and 0 , so I get 220 foo 200 bar , etc. How can I fix this so that it just finds "20"

here is the code i have

 num = raw_input ("Type Number : ") search = open("file.txt") for line in search: if num in line: print line 

Thanks.

+4
source share
6 answers

To check the exact match, you should use num == line . But line has an end-of-line character \n or \r\n , which will not be in num , since raw_input separates the ending newline. Therefore, it may be convenient to remove all spaces at the end of line with

 line = line.rstrip() 

 with open("file.txt") as search: for line in search: line = line.rstrip() # remove '\n' at end of line if num == line: print(line ) 
+6
source

you must use regular expressions to find everything you need:

 import re p = re.compile(r'(\d+)') # a pattern for a number for line in file : if num in p.findall(line) : print line 

the regular expression will return you all the numbers in the string as a list, for example:

 >>> re.compile(r'(\d+)').findall('123kh234hi56h9234hj29kjh290') ['123', '234', '56', '9234', '29', '290'] 

so that you do not match "200" or "220" for "20".

+2
source

It is very simple:

 numb = raw_input('Input Line: ') fiIn = open('file.txt').readlines() for lines in fiIn: if numb == lines[0]: print lines 
+1
source

Make lists of matching lines - several options:

 def lines_that_equal(line_to_match, fp): return [line for line in fp if line == line_to_match] def lines_that_contain(string, fp): return [line for line in fp if string in line] def lines_that_start_with(string, fp): return [line for line in fp if line.startswith(string)] def lines_that_end_with(string, fp): return [line for line in fp if line.endswith(string)] 

Create a consistent string generator (memory efficient):

 def generate_lines_that_equal(string, fp): for line in fp: if line == string: yield line 

Print all the relevant lines (first find all matches, then print them):

 with open("file.txt", "r") as fp: for line in lines_that_equal("my_string", fp): print line 

Print all the relevant lines (print them lazily, as we find them)

 with open("file.txt", "r") as fp: for line in generate_lines_that_equal("my_string", fp): print line 

Generators (created with the output) are your friends, especially with large files that do not fit into memory.

+1
source

The check should be like this:

 if num == line.split()[0]: 

If the .txt file has this layout:

 1 foo 20 bar 30 20 

We split "1 foo" into ['1', 'foo'] and just use the first element, which is a number.

0
source
 num = raw_input ("Type Number : ") search = open("file.txt","r") for line in search.readlines(): for digit in num: # Check if any of the digits provided by the user are in the line. if digit in line: print line continue 
0
source

All Articles