【发布时间】:2021-06-09 16:59:45
【问题描述】:
我正在尝试提取文本文件第一行中出现的第一个数字。我是菜鸟,所以我在玩正则表达式。我遇到的问题是什么都没有打印,所以我不确定是我的代码还是其他什么?
我也尝试过打印我的文件名,但也没有任何反应,所以我不确定发生了什么
work_dir = "User/...my folder of 9 text files"
for path in glob.glob(os.path.join(work_dir, "*.txt")):
with io.open(path, mode="r", encoding="utf-8") as file:
first_line = file.readline()
for line[34:] in first_line:
if "LOCUS" in line[0:34]:
matches = int(re.search(r"(\d+)", first_line).group(0))
print(matches)
name = os.path.basename(path).replace(".gbff", "")
print(name)
这是我正在使用的文件类型的示例的开头。 它是一个文本文件,尽管它在这里看起来像一个表格。
LOCUS AE017334 *5227419* bp DNA circular BCT 03-DEC-2015
DEFINITION Bacillus anthracis str. 'Ames Ancestor', complete genome.
ACCESSION AE017334
VERSION AE017334.2
DBLINK BioProject: PRJNA10784
BioSample: SAMN02603433
我需要我放在**周围的号码
【问题讨论】:
-
包含内容的文件的最小样本会有所帮助
-
我贴了一段摘录 :) @kamal
标签: python regex file printing