使用 python 在文件中添加缺失的行
Add missing lines in file with python
在编程和 python 等方面,我是初学者。
如果这是一个简单的问题,我们深表歉意。
但是我有一些大文件,例如包含这样的行:
10000 7
20000 1
30000 2
60000 3
我想要的是一个还包含 'missing' 行的文件,如下所示:
10000 7
20000 1
30000 2
40000 0
50000 0
60000 3
这些文件相当大,因为我正在处理全基因组序列数据。第一列基本上是基因组中的一个位置,第二列是我在 10kb window 中找到的 SNP 数量。但是,我认为这些信息甚至都不相关,我只想编写一个简单的 python 代码,使用 if else 语句将这些行添加到文件中。
所以如果位置与上一行+10000的位置不匹配,则写入'missing line',否则写入正常出现的行。
我只是预见到其中的一个问题,即当连续几行丢失时(如我的示例)。
对于这个简单的问题,有人有聪明的解决方案吗?
非常感谢!
我会按照以下几行建议一个程序。你跟踪你最后看到的基因组位置(我猜一开始是 0)。然后您从输入文件中逐行读取。对于每一个,您首先输出任何缺失的行(从先前的基因组位置 + 10kb,以 10kb 为步长,到您阅读的新行之前的 10kb),然后是您刚刚阅读的行。
换句话说,您缺少的一点是当 "the position does not match the position of the previous line + 10000" 时,您应该有一个小循环来生成丢失的输出,而不是只写出一行。 (在你真正开始编写代码之前,下面的评论可能没有意义:你实际上不需要测试位置是否匹配;如果你写对了,你会发现当它匹配时你的循环输出没有额外的行)
出于各种原因,这里通常的做法是不为您编写代码:-),但希望以上内容对您有所帮助。
from collections import defaultdict
d = defaultdict(int)
with open('file1.txt') as infile:
for l in infile:
pos, count = l.split()
d[int(pos)] = int(count)
with open('file2.txt') as outfile:
for i in range(10000, pos+1, 10000):
outfile.write('{}\t{}'.format(i, d[i]))
这是一个快速版本。我们将文件读入defaultdict
。当我们稍后访问这些值时,任何没有关联值的键都将获得默认值零。然后我们取 10000
到 pos
范围内的每个数字,其中 pos
是第一个文件中的最后一个位置,步长为 10000
。我们在 defaultdict
中访问这些值并将它们写入第二个文件。
这个怎么样:
# Replace lines.txt with your actual file
with open("lines.txt", "r") as file:
last_line = 0
lines = []
for line in file:
num1, num2 = [int(i) for i in line.split("\t")]
while num1 != last_line + 10000:
# A line is missing
lines.append((last_line + 10000, 0))
last_line += 10000
lines.append((num1, num2))
last_line = num1
for num1, num2 in lines:
# You should print to a different file here
print(num1, num2)
您可以将值写入新文件,而不是最后一个打印语句。
编辑: 我 运行 此示例中的此代码。输出如下。
lines.txt
10000 7
20000 1
30000 2
60000 3
输出
10000 7
20000 1
30000 2
40000 0
50000 0
60000 3
我会使用 defaultdict
它将使用 0
作为默认值
因此,您会将文件读取到此 defaultdict
,而不是读取它(手动处理密钥)并将其写回文件。
看起来有点像这样
from collections import defaultdict
x = defaultdict(int)
with open(filename) as f:
data = x.split()
x[data[0]] = x[data[-1]]
with open(filename, 'w') as f:
for i in range(0, max(x.keys())+1, 10000):
f.write('{}\t{}\n'.format(i, x[i]))
在编程和 python 等方面,我是初学者。 如果这是一个简单的问题,我们深表歉意。
但是我有一些大文件,例如包含这样的行:
10000 7
20000 1
30000 2
60000 3
我想要的是一个还包含 'missing' 行的文件,如下所示:
10000 7
20000 1
30000 2
40000 0
50000 0
60000 3
这些文件相当大,因为我正在处理全基因组序列数据。第一列基本上是基因组中的一个位置,第二列是我在 10kb window 中找到的 SNP 数量。但是,我认为这些信息甚至都不相关,我只想编写一个简单的 python 代码,使用 if else 语句将这些行添加到文件中。
所以如果位置与上一行+10000的位置不匹配,则写入'missing line',否则写入正常出现的行。
我只是预见到其中的一个问题,即当连续几行丢失时(如我的示例)。 对于这个简单的问题,有人有聪明的解决方案吗?
非常感谢!
我会按照以下几行建议一个程序。你跟踪你最后看到的基因组位置(我猜一开始是 0)。然后您从输入文件中逐行读取。对于每一个,您首先输出任何缺失的行(从先前的基因组位置 + 10kb,以 10kb 为步长,到您阅读的新行之前的 10kb),然后是您刚刚阅读的行。
换句话说,您缺少的一点是当 "the position does not match the position of the previous line + 10000" 时,您应该有一个小循环来生成丢失的输出,而不是只写出一行。 (在你真正开始编写代码之前,下面的评论可能没有意义:你实际上不需要测试位置是否匹配;如果你写对了,你会发现当它匹配时你的循环输出没有额外的行)
出于各种原因,这里通常的做法是不为您编写代码:-),但希望以上内容对您有所帮助。
from collections import defaultdict
d = defaultdict(int)
with open('file1.txt') as infile:
for l in infile:
pos, count = l.split()
d[int(pos)] = int(count)
with open('file2.txt') as outfile:
for i in range(10000, pos+1, 10000):
outfile.write('{}\t{}'.format(i, d[i]))
这是一个快速版本。我们将文件读入defaultdict
。当我们稍后访问这些值时,任何没有关联值的键都将获得默认值零。然后我们取 10000
到 pos
范围内的每个数字,其中 pos
是第一个文件中的最后一个位置,步长为 10000
。我们在 defaultdict
中访问这些值并将它们写入第二个文件。
这个怎么样:
# Replace lines.txt with your actual file
with open("lines.txt", "r") as file:
last_line = 0
lines = []
for line in file:
num1, num2 = [int(i) for i in line.split("\t")]
while num1 != last_line + 10000:
# A line is missing
lines.append((last_line + 10000, 0))
last_line += 10000
lines.append((num1, num2))
last_line = num1
for num1, num2 in lines:
# You should print to a different file here
print(num1, num2)
您可以将值写入新文件,而不是最后一个打印语句。
编辑: 我 运行 此示例中的此代码。输出如下。
lines.txt
10000 7
20000 1
30000 2
60000 3
输出
10000 7
20000 1
30000 2
40000 0
50000 0
60000 3
我会使用 defaultdict
它将使用 0
作为默认值
因此,您会将文件读取到此 defaultdict
,而不是读取它(手动处理密钥)并将其写回文件。
看起来有点像这样
from collections import defaultdict
x = defaultdict(int)
with open(filename) as f:
data = x.split()
x[data[0]] = x[data[-1]]
with open(filename, 'w') as f:
for i in range(0, max(x.keys())+1, 10000):
f.write('{}\t{}\n'.format(i, x[i]))