0
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

1
  • You've already written the code. What do you expect us to do? Oct 21, 2013 at 9:16

3 Answers 3

2

If convertSeq works then this should do:

line_num = 1
for line in open(filename):
    print convertSeq(line, line_num)
    line_num += 1
4
  • Great its nicely working, if these (00001000000000000000000010000000000000000000100000000000000000001000000000000000) lines are output of a function and rather storing it in a file i directly want to use function convertSeq(s, index) so that its directly generate output 1 1:5 1:25 1:45 1:65 so what would be the changes in given code thanks a lot
    – JAZs
    Oct 21, 2013 at 9:18
  • @ mshsayem Great its nicely working, if these (0000100000000000000000001000000000000000000010000000000000000000100000000000000‌​0) lines are output of a function and rather storing it in a file i directly want to use function convertSeq(s, index) so that its directly generate output 1 1:5 1:25 1:45 1:65 so what would be the changes in given
    – JAZs
    Oct 21, 2013 at 9:22
  • If the input string (say st from a function) contains newline characters (\n) then you can call st.split('\n'), enumerate the resulting list and then call convertSeq. ... I am a bit confused what you really want..
    – mshsayem
    Oct 21, 2013 at 9:23
  • @ mshsayem actually this code is working fine but first i convert ACDE into this 0000100000000000000000001000000000000000000010000000000000000000100000000000000‌​0 with a script and save to it in a text file now i have to run next script what u have write for me to get the out put "1 1:5 1:25 1:45 1:65" i just want a single code rather doing this task with two different code. is it possible
    – JAZs
    Oct 21, 2013 at 9:30
1

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)
1

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
5
  • I missed the line number part from your questions, so you can use enumerate() as suggested by @Duncan. Updated my code to reflect it
    – DevC
    Oct 21, 2013 at 9:34
  • 1
    cool, please consider accepting answer if this is what you were looking for
    – DevC
    Oct 21, 2013 at 9:36
  • actually this code is working fine but first i need to convert ACDE into this 0000100000000000000000001000000000000000000010000000000000000000100000000000000‌‌​​0 with a script and save to it in a text file now i have to run next script what u have write for me to get the out put "1 1:5 1:25 1:45 1:65" i just want a single code rather doing this task with two different code. so i want to know what whould be the code if rather write binary string in file i directly save it in a variable and will directly get the out put 1:5 1:25 1:45 1:65
    – JAZs
    Oct 21, 2013 at 9:40
  • what is the other script, is that python?? sorry but what is ACDE??
    – DevC
    Oct 21, 2013 at 9:41
  • @ DevC yes its python script bin_dict = { 'A':'10000000000000000000', 'C':'01000000000000000000', 'D':'00100000000000000000', 'E':'00010000000000000000', '\n':'' } def binary_conv(sequen): a = ("".join([bin_dict[l] for l in seq])) return a for seq in lines: a = binary_conv(seq) print a
    – JAZs
    Oct 21, 2013 at 9:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.