python2.7文件读取和spli

2024-04-20 07:34:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从一个文件(exon)中读取和拆分值_坐标.txt)基于值“,”。我写的剧本是这样的:

input_dna = "input_dna.txt"
file_1 = open (input_dna).read().rstrip("\n")
exon_coordinates = "exon_coordinates.txt"
file_2 = open (exon_coordinates).read().rstrip("\n")

for line in file_2:
    print (line)
    positions = line.split (',')
    print (positions)
    start = int(positions [0])
    stop = int(positions [1])
    exon = file_1 [start:stop]

外显子_坐标.txt包含如下值:

0,10
19,27
38,44

input_dna.txt包含如下DNA字符串:

ACTGATCGATTACGTATAGTAGAATTCTATCATACATATATATCGATGCGTTCATCTGATCGA

但是,当我打印位置时,输出如下:

0
['0']
,
['', '']
1
['1']
0
['0']

['\n']
1
['1']
9
['9']
,
['', '']
2
['2']
7
['7']

['\n']
3
['3']
8
['8']
,
['', '']
4
['4']
4
['4']

我是不是在剧本上出错了,还是在外显子上出了什么问题_坐标.txt我创建的文件? 我是一个生物学家,以前没有任何编写脚本的经验。我的可能是一个非常基本的问题,但我真的很感谢你的帮助。 提前多谢了。你知道吗


Tags: 文件txtreadinputlineopenstartdna
1条回答
网友
1楼 · 发布于 2024-04-20 07:34:19

您将字符串(“read”的结果)与字符串列表混合(我认为您期望从for line in file_2得到什么) 也许这就是你读文件2的意思:

with open (exon_coordinates) as file_2:
    for line in file_2:
        positions = line.strip().split(',')

相关问题 更多 >