【问题标题】:Most efficient way to edit the first line of a large input stream one manner, and all other lines a different manner?以一种方式编辑大型输入流的第一行的最有效方法,而以不同的方式编辑所有其他行?
【发布时间】:2017-04-10 23:01:24
【问题描述】:

问题... (N=2*10^7)

从此出发:

colName1 colName2 colName3 ... colNameN
1        x        x        ... x 
2        x        x        ... x
1        y        x        ... x
2        y        x        ... x  
...      ...      ...      ... ...
1        xx       xx       ... xx
2        xx       xx       ... xx

到这里:

Sample colName1 colName2 colName3 ... colNameN
A       1        x        x       ... x 
A       2        x        x       ... x
B       1        y        x       ... x
B       2        y        x       ... x  
...     ...      ...      ...     ... ...
N       1        xx       xx      ... xx
N       2        xx       xx      ... xx

问题: 我需要将“示例”添加到第一个“标题”行,并将相应的示例名称添加到之后的每一行。样本名称将存储在一个对象中。

令人困惑的问题:

  • 数据来自输入流;目前通过 subprocess.PIPE 处理
  • 文件有 2000 万行是很常见的,所以每次检查 firstLine 标志会很昂贵?

我想知道是否有办法只对输入流中的第一行输入做一些事情。

或者……

将所有行都尝试相同会更容易吗,这意味着我们将示例名称添加到标题行。然后,我们将文件中的第一个单词从样本名称编辑为“Sample\t”

这种方法的成本如何? 目前,我有一个 firstLine 标志,如下所示。

fileSTREAM = subprocess.Popen(callString, stdout=subprocess.PIPE, shell=True)

# To indicate the first line of the steam, which happens to be the column-headers.
firstLine = True

# Foreach to add a word to the front of each line of input.
for line in fileSTREAM.stdout:

    # Decode the input from btye literals to strings.
    currLine = line.decode("utf-8")

    # First line is different, we want to add SAMPLE, instead of the actual sample name.
    if firstLine == True:
        outputTARGET.write("SAMPLE \t%s" % currLine)
        firstLine = False

    # All other lines we want to add the sample name, instead of the word SAMPLE.
    else:
        outputTARGET.write(str(wildcards.samples) + "\t%s" % currLine)

可能不是特定于 python 的问题,但我正在寻找特定于 python 的解决方案。

【问题讨论】:

  • 为什么不简单地从 fileSTREAM 中读取第一行,处理它(包括“Sample”)然后然后进入你的循环(**for line in fileSTREAM.stdout)?
  • 还要注意 if firstLine == True 是多余的;只有 如果 firstLine 可以。
  • 如果我只读第一行,你推荐什么函数可以读到第一个“\n”字符。读线()?编辑2:在其他地方重新编写测试。我明白你的意思。编辑 3:要跑去上课参加期末考试,今晚将结束主题!
  • 是的,你没看错。

标签: python processing-efficiency


【解决方案1】:

向@Prune 大声喊叫,谢谢:)

最好的方法是读取输入流的第一行。 Python 有很好的内置函数来处理这个问题。

结束了这个:

# Call the function and capture its output to modify each line.
fileSTREAM = subprocess.Popen(callString, stdout=subprocess.PIPE, shell=True)

# Initially read and edit just the first, adding 'SAMPLE' to header line.
outputTARGET.write("SAMPLE \t%s" % fileSTREAM.stdout.readline().decode("utf-8"))

# Add the sampleName to each line after the header line.
for line in fileSTREAM.stdout:
    # Decode the input from btye literals to strings
    outputTARGET.write(str(wildcards.samples) + "\t%s" % line.decode("utf-8"))

【讨论】:

  • 还在学习 StackOverflow。一个后续问题......结束这个话题的最佳方式是什么?我接受了 Prune 的建议,然后更新了我的代码。我已经发布了我满意的结果代码。我明天就选择这个作为答案吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-07
  • 1970-01-01
  • 2020-10-16
相关资源
最近更新 更多