Domanda

I'm trying to build a bytearray out of a binary file as follows:

myArr = bytearray()
with open(r"C:\Users\User\MyFileName.bin", "rb") as f:
        byte = f.read(1)
        while byte:
            myArr.extend(byte)
            byte = f.read(1)

This results in:

Traceback (most recent call last):
  File "myScriptName.py", line 20, in testByteParsing
    myArr.extend(byte)
TypeError: unicode argument without an encoding

What is wrong in the above?

FWIW, I'm using IronPython

È stato utile?

Soluzione

By looking at the IronPython's bytearray source code I could see that extend method internally calls GetBytes method:

public void extend(object seq) {
    extend(GetBytes(seq));
}

and that actually, GetBytes method is hardwired to except when a string is given.

private static IList<byte>/*!*/ GetBytes(object/*!*/ value) {
    ListGenericWrapper<byte> genWrapper = value as ListGenericWrapper<byte>;
    if (genWrapper == null && value is IList<byte>) {
        return (IList<byte>)value;
    }

    if (value is string || value is Extensible<string>) {
        throw PythonOps.TypeError("unicode argument without an encoding");
    }

    List<byte> ret = new List<byte>();
    IEnumerator ie = PythonOps.GetEnumerator(value);

    while (ie.MoveNext()) {
        ret.Add(GetByte(ie.Current));
    }
    return ret;
    }

So I would recommend to convert the byte read from the file to an integer value within 0-255 with the ord() function and then use append to insert it in the bytearray.

Your code can remain like this:

myArr = bytearray()
with open(r"C:\Users\User\MyFileName.bin", "rb") as f:
    byte = f.read(1)
    while byte:
        myArr.append(ord(byte))
        byte = f.read(1)

That's somehow ugly, I think they implemented this this way in compliance with some .NET library's restraints.

Perhaps someone more familiarized with IronPython can provide a more elegant solution.

Hope this helps!

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top