这个方法输入一个文件和文件的目录。它包含一个数据矩阵,需要复制每行的前20列,在给定的行名称和行的相应字母之后。每个文件的前3行被跳过,因为它有不需要的不重要的信息,也不需要文件底部的数据
例如,文件如下所示:
unimportant information--------
unimportant information--------
-blank line
1 F -1 2 -3 4 5 6 7 (more columns of ints)
2 L 3 -1 3 4 0 -2 1 (more columns of ints)
3 A 3 -1 3 6 0 -2 5 (more columns of ints)
-blank line
unimportant information--------
unimportant information--------
方法的输出需要以某种给定的形式打印出一个“矩阵”
到目前为止,输出以字符串的形式给出了每一行的列表,但是我正在尝试找出解决问题的最佳方法。我不知道如何忽略文件末尾不重要的信息。我不知道如何只检索每行字母后的前20列,也不知道如何忽略行号和行号
def pssmMatrix(self,ipFileName,directory):
dir = directory
filename = ipFileName
my_lst = []
#takes every file in fasta folder and put in files list
for f in os.listdir(dir):
#splits the file name into file name and its extension
file, file_ext = os.path.splitext(f)
if file == ipFileName:
with open(os.path.join(dir,f)) as file_object:
for _ in range(3):
next(file_object)
for line in file_object:
my_lst.append(' '.join(line.strip().split()))
return my_lst
预期结果:
['-1 2 -3 4 5 6 7'], ['3 -1 3 4 0 -2 1'], ['3 -1 3 6 0 -2 5']
实际结果:
['1 F -1 2 -3 4 5 6 7'], ['2 L 3 -1 3 4 0 -2 1'], ['3 A 3 -1 3 6 0 -2 5'], [' '], [' unimportant info'], ['unimportant info']
试试这个办法
好吧,在我看来,你有一个文件,里面有你想要的行,你想要的行总是以一个数字和一个字母开头。所以我们能做的就是对它应用一个正则表达式,只得到与该模式匹配的行,并且只得到模式后面的数字
这个表达式看起来像
(?<=[0-9]\s[A-Z]\s)[0-9\-\s]+
希望有帮助
相关问题 更多 >
编程相关推荐