-2

基因组注释存储在大的纯文本文件中,例如https://www.ebi.ac.uk/ena/data/view/FO203355&display=textc

我只想提取以“FT”开头的行。由于我需要提取数千个这些文件的“FT”,因此下载整个文件并手动提取所需的行是不可行的。

是否有任何终端或 python 构造来执行此操作?我最终想创建一个大型(python)熊猫数据框。

4

2 回答 2

0

由于您最终打算使用pandas,因此您只需将数据流式传输到您的脚本并过滤您需要的行。最简单的方法是requests在流模式下使用模块,然后将远程数据视为文件流,即:

import requests

url = "https://www.ebi.ac.uk/ena/data/view/FO203355&display=text"

with requests.get(url, stream=True) as r:  # open a streaming request
    for line in r:  # iterate over the stream line by line
        if line[:2] == "FT":  # check if a line begins with `FT`
            print(line)  # or do whatever you want with the line

如果您只想保存数据,可以将过滤后的行转发到文件输出流:

import requests

url = "https://www.ebi.ac.uk/ena/data/view/FO203355&display=text"

with requests.get(url, stream=True) as r, open("output.dat", "w") as f:
    for line in r:  # iterate over the stream line by line
        if line[:2] == "FT":  # check if a line begins with `FT`
            f.write(line)  # write the line to output.dat

您可能想要创建数据框并直接将行解析到其中,但这取决于您要如何解析数据,所以这是我将留给您的练习。

于 2018-05-15T10:53:26.803 回答
-1

您可以使用 curl 和 grep。您仍然必须下载整个文件,除非 ebi.ac.uk 服务器 API 提供服务器端过滤。

curl 'https://www.ebi.ac.uk/ena/data/view/FO203355&display=text' | grep '^FT' > lines.txt
于 2018-05-15T10:34:52.817 回答