简体   繁体   中英

Why isn't readline() working properly?

I have a file called untitled.txt with the following lines:

Line 1: ATTCTGGA

Line 2: CGCCCGAATCCAGAACGCATTCCCATATTTCGGGACCACTGGCCTCCACGGTACGGACGTCAATCAAAT

When I enter code for finding the positions where sp (line 1) appears in p (line 2) with a maximum of d errors, I get the output [27], which is only one of the correct positions.

code using only readline(): 在此处输入图片说明

When I define "sp = 'ATTCTGGA'" directly within the code, however, I get [6, 7, 26, 27] , which is the correct answer.

在此处输入图片说明

Why does "sp = text.readline()" not get the same result?

Because readline() provides the whole line, including the trailing newline character. You should strip the trailing newline:

sp = text.readline().rstrip("\n") 
p = text.readline().rstrip("\n")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM