重新编码二进制文件

时间:2015-06-23 12:50:17

标签: python

我有一个包含此内容的文件

1 5 9 14 15  
00000
10000
00010
11010
00010

我想解析文件以便输出以下内容

UUUUUUUUUUUUUU
YUUUUUUUUUUUUU
UUUUUUUUUUUUYY
YUUUYUUUUUUUYU
UUUUUUUUUUUUYU

这意味着第一行提供了一个位置。如果有0,则变为U.如果是1则变为Y.在前两列之间有4个未映射的cols,这意味着对于这四个cols,所有行都是U - 和0

我在python中尝试了以下内容

    #!/usr/bin/env python2
import sys
with open(sys.argv[1]) as f:
    f.readline()
    for line in f:
        new = ''
        for char in line.rstrip():
            if char == '0':
                new += 'UU'
            elif char == '1':
                new +='YU'
        print new.rstrip()[:-1]

问题是这个脚本只有在位置相距2的情况下才有效,但它们也可以更大 - 如何扩展脚本?

当我运行代码时,有一些poroblem,Delimity - 得到一个错误

dropbox.com/s/cf8rbv20bgyvssq/conv_inp?dl=0 这些是真正的da

Traceback (most recent call last):
  File "./con.py", line 8, in <module>
    for v in xrange(max(positions) + 1):
OverflowError: long int too large to convert to int

2 个答案:

答案 0 :(得分:1)

只是一个猜测。

实施转换器:

def convert(s):
    return "UUU".join({"0": "U", "1": "Y"}[c] for c in s[:-1]) + "U"

测试一下:

assert convert("00000") == "UUUUUUUUUUUUUU"
assert convert("10000") == "YUUUUUUUUUUUUU"
assert convert("00010") == "UUUUUUUUUUUUYU"
assert convert("11010") == "YUUUYUUUUUUUYU"
assert convert("00010") == "UUUUUUUUUUUUYU"

答案 1 :(得分:0)

检查此代码:

#!/usr/bin/env python2
import sys

def myxrange(to):
    x = 0
    while x < to:
        yield x
        x += 1

with open(sys.argv[1]) as f:
    positions = map(lambda x: long(x) - 1, f.readline().split())
    max_pos = max(positions)
    for line in f:
        new = ''
        for i in myxrange(max_pos + 1):
            if i in positions and line[positions.index(i)] == '1':
                new += 'Y'
            else:
                new += 'U'
        print new.rstrip()