首先,我是一个完全的编码新手,不知道自己在做什么
我正在使用一个数据库txt文件,并已将其导入并打开。我现在需要遍历文件,找到一个特定的关键字(数字),并将其打印到一个新文件中。我一直在试图理解编码,但毫无结果。有人能给我解释一下怎么做吗。请低声解释,让我这样的白痴明白
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'))```
使用readlines将txt文件逐行加载到字符串列表中
现在是循环:
基于您的问题,您实际上希望在字符串中执行“模式搜索”。 为此,只需使用循环示例中的相同代码并插入“模式搜索”函数,逐行检查txt文件中是否存在您的模式
如果要将其打印到文件中,只需更改我制作的打印代码行。
在此处查看有关写入功能的更多信息:
https://www.w3schools.com/python/python_file_write.asp
根据有关代码的新信息,尝试以下操作:
这将用你想要的模式写入你的文件
相关问题 更多 >
编程相关推荐