我如何纠正我的python代码将二进制字符串转换为位置特定的代码

时间:2013-10-21 08:58:15

标签: python binary

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

已创建functoin以进行转换

"00001000000000000000000010000000000000000000100000000000000000001000000000000000" 

到位置特定代码,如果我使用

,它的工作正常
seq1 = "00001000000000000000000010000000000000000000100000000000000000001000000000000000"

作为我得到的代码的输入,

 1 1:5 1:25 1:45 1:65 

作为输出(如预期的那样)。但是当我使用带有内容的输入文件时:

00001000000000000000000010000000000000000000100000000000000000001000000000000000 
10000000000000000000001000000000000000000010000000000000000000100000000000000000 
00100000000000000000001000000000000000000010000000000000000000100000000000000000

输出非常奇怪。

对我而言,当我使用

seq1="00001000000000000000000010000000000000000000100000000000000000001000000000000000"

由于" "而被视为字符串,当我使用输入文件时,它将01视为单独的字符。

我应该使用什么方法,以便它可以从包含二进制代码的文件中输入二进制代码,并为每一行生成输出。

示例输入:

00001000000000000000000010000000000000000000100000000000000000001000000000000000
10000000000000000000001000000000000000000010000000000000000000100000000000000000
00100000000000000000001000000000000000000010000000000000000000100000000000000000
00001000000000000000000010000000000000000000100000000000000000001000000000000000
10000000000000000000001000000000000000000010000000000000000000100000000000000000
00100000000000000000001000000000000000000010000000000000000000100000000000000000

示例输出:

1 1:5 1:25 1:45 1:65
2 2:1 2:21 2:44 2:64
and so on...........

由于我是编程新手,所以我花了5-6个小时但没有成功。请帮助

3 个答案:

答案 0 :(得分:2)

如果convertSeq有效,那么应该这样做:

line_num = 1
for line in open(filename):
    print convertSeq(line, line_num)
    line_num += 1

答案 1 :(得分:1)

你可以这样做,将整个文件作为字符串读取并传递给你的函数

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

根据您提供的内容,它提供以下输出

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

您还可以将文件名作为arg读入您的程序,这样您就不必使用硬编码文件名

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

答案 2 :(得分:1)

使用enumerate()对文件进行迭代以计算行数:

with open(filename) as f:
    for line_no, seq in enumerate(f, start=1):
        print convertSeq(seq, line_no)