二进制fi的记录

2024-03-29 06:55:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个文件

1 5 9 14 15  
00000
10000
00010
11010
00010

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

^{pr2}$

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

我在python中尝试了以下方法

^{3}$

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

当我从运行代码时有一些问题,定界-得到一个错误

在dropbox.com/s/cf8rbv20bgyvsq/conv_输入?dl=0 这些才是真正的检察官

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

Tags: 文件to方法代码in脚本com错误
2条回答

检查此代码:

#!/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()

只是一个猜测。在

实施转换器:

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

测试一下:

^{pr2}$

相关问题 更多 >