【发布时间】: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