简体   繁体   中英

Python 3: How to loop through a text file and find matching keywords?

To start, I am a complete newb to coding and don't know what I'm doing.

I am working with a database txt file and have got it imported and open. I need to now loop through the file, find a specific keyword (number), and print this out to a new file. I have tried endlessly to understand coding to no avail. Can someone explain how to do this to me. Please explain in a dumbed down way so an idiot like me can understand.

 file1 = open('database.txt', 'r')
 Lines = file1.readlines()

pattern = "gene_numbers_here"

for line in Lines:
   
  
   if pattern in line:
      print(..., file = open("gene1found.txt",'w'))```
  

Use readlines to load up the txt file line by line into a list of strings

file1 = open('myfile.txt', 'r')
Lines = file1.readlines()

Now for the looping:

for line in Lines:
   print(line)

Based on your problem, you are actually wanting to do a "pattern search" in a string. For that, just use the same code from the looping example and insert a "pattern search" function to check if your pattern exists in your txt file, line by line.

# declare the pattern
pattern = "this_pattern_only"

# loop through the list of strings in Lines
for line in Lines:
   
   # patter search statement
   if pattern in line:
      print("pattern exist")
   else:
      print("pattern does not exist")

If you want to print this to a file, just change the print code lines I made.
Check out more on the write functionalities here:
https://www.w3schools.com/python/python_file_write.asp

Based on you new info about the code, try this:

# file1 is database, file2 is output
file1 = open('database.txt', 'r')
file2 = open('gene1found.txt', 'w')

Lines = file1.readlines()
pattern = "gene_numbers_here"


# search and write lines with gene pattern
print("Searching database ...")

for line in Lines:  
   if pattern in line:
      file2.write(line)

print("Search complete !")


# close the file
file1.close()
file2.close()

This will write the gene lines with the pattern you want to your file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM