使用python解析文本文件以获取ID

时间:2016-12-07 22:45:12

标签: python regex parsing text

我已经下载了大量数据并将其存储在文本文件中。我还有一份概述文件。这里可以看到一个例子:

Without x terms:0
ID's: 

Without y terms: 15
ID's: 10362383
10390455
10658293
10658295
10868884
10947144
11015024
11430394
11674903
11773985
7762512
7934452
8879328
9186393
9812933

我需要在没有y术语的情况下循环访问ID,但是我不确定如何解析文本文件以获取这些ID。到目前为止,我已经写了这个,但我不确定如何继续。

file = '...'
file_object = open(file, 'r')
text = file_object.read()
print (text)

2 个答案:

答案 0 :(得分:0)

您必须解析文件中的每一行。您必须创建for-each循环:

for each line in file:

这会将文件按行划分,并且应该在没有y术语的情况下遍历ID。请记住,这也会拆分文件的顶部(非y术语)。

答案 1 :(得分:0)

path_file = '...'
f = open(path_file, 'r').read()
ids_string = f.split("Without y terms:")[1].split("ID's:")[1].split()
ids_integer = [int(e) for e in ids_string]
print (ids_integer)