Reputation: 29
def convertSeq(s, index):
result = [i+1 for i,ch in enumerate(s) if ch=='1']
result = ' '.join([str(index)+':'+str(i) for i in result])
result = str(index)+' '+result
return result
seq1 = "00001000000000000000000010000000000000000000100000000000000000001000000000000000"
a = convertSeq(seq1, 1)
print a
Given functoin has been created to convert
"00001000000000000000000010000000000000000000100000000000000000001000000000000000"
into position specific code its work fine if i use
seq1 = "00001000000000000000000010000000000000000000100000000000000000001000000000000000"
as input for the code I get,
1 1:5 1:25 1:45 1:65
as output (as expected). But when I used an input file with the contents:
00001000000000000000000010000000000000000000100000000000000000001000000000000000
10000000000000000000001000000000000000000010000000000000000000100000000000000000
00100000000000000000001000000000000000000010000000000000000000100000000000000000
the output is very weird.
To me it seems that when i use
seq1="00001000000000000000000010000000000000000000100000000000000000001000000000000000"
it is considered a string because of " "
and when I use a input file so it considers 0
and 1
as individual charactera.
What method should I use so that it can take binary code input from a file containing binary code in lines and generate output for each and every line.
Sample Input:
00001000000000000000000010000000000000000000100000000000000000001000000000000000
10000000000000000000001000000000000000000010000000000000000000100000000000000000
00100000000000000000001000000000000000000010000000000000000000100000000000000000
00001000000000000000000010000000000000000000100000000000000000001000000000000000
10000000000000000000001000000000000000000010000000000000000000100000000000000000
00100000000000000000001000000000000000000010000000000000000000100000000000000000
Sample Output:
1 1:5 1:25 1:45 1:65
2 2:1 2:21 2:44 2:64
and so on...........
As I am very new in programming I have spent my 5-6 hours on it but not succeeded.Please help
Upvotes: 0
Views: 150
Reputation: 7423
you can do like this, read the whole file as string and pass to your function
def convertSeq(s, index):
result = [i+1 for i,ch in enumerate(s) if ch=='1']
result = ' '.join([str(index)+':'+str(i) for i in result])
result = str(index)+' '+result
return result
# read the sequence from file
with open ("file.txt",'r') as f:
f_seq=f.readlines()
for line, seq in enumerate(f_seq, start=1):
a = convertSeq(seq, line)
print a
From the content you have given, it gives following output
1 1:5 1:25 1:45 1:65
2 2:1 2:23 2:43 2:63
3 3:3 3:23 3:43 3:63
4 4:5 4:25 4:45 4:65
5 5:1 5:23 5:43 5:63
6 6:3 6:23 6:43 6:63
You can also read the file name as arg to your program, so you don't have to hard code file name
import sys
def convertSeq(s, index):
result = [i+1 for i,ch in enumerate(s) if ch=='1']
result = ' '.join([str(index)+':'+str(i) for i in result])
result = str(index)+' '+result
return result
#take the file name as arg
seqFile = sys.argv[1]
with open (seqFile,'r') as f:
f_seq=f.readlines()
for line, seq in enumerate(f_seq, start=1):
a = convertSeq(seq, line)
print a
Upvotes: 1
Reputation: 95692
Iterate over the file using enumerate()
to count the lines:
with open(filename) as f:
for line_no, seq in enumerate(f, start=1):
print convertSeq(seq, line_no)
Upvotes: 1
Reputation: 18018
If convertSeq
works then this should do:
line_num = 1
for line in open(filename):
print convertSeq(line, line_num)
line_num += 1
Upvotes: 2