Python Forum

Full Version: Encode string split problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Traceback (most recent call last):
File "C:/-Prsnl/learning/python/VzPyinP/vzPylib/vzPyfunc.py", line 141, in <module>
print(" The Return of Get Struct Splicer {%s}" % format(get_array_struct(lines = samplelines)))
File "C:/-Prsnl/learning/python/VzPyinP/vzPylib/vzPyfunc.py", line 135, in get_array_struct
fields = parse(x = lined)
File "C:/-Prsnl/learning/python/VzPyinP/vzPylib/vzPyfunc.py", line 130, in <lambda>
parse = lambda x: tuple(s.decode() for s in unpack(x.encode()))
struct.error: unpack_from requires a buffer of at least 275 bytes


def get_array_struct(lines):
    start_time = time.time()
    fieldwidths = (2, 10, 24,10,20,20,30,20,20,10,20,89)  # negative widths represent ignored padding fields
    fmtstring = ''.join('{}{}'.format(abs(fw), 'x' if fw < 0 else 's')
                        for fw in fieldwidths)
    fieldstruct = struct.Struct(fmtstring)

    if sys.version_info[0] < 3:
        parse = fieldstruct.unpack_from
    else:
        # converts unicode input to byte string and results back to unicode string
        unpack = fieldstruct.unpack_from
        parse = lambda x: tuple(s.decode() for s in unpack(x.encode()))
        # print('fmtstring: {!r}, recsize: {} chars'.format(fmtstring, fieldstruct.size))
    line_array_dic_map = []

    for lined in lines:
        fields = parse(x = lined)
        line_array_dic_map.append(fields)

    print("get_array_struct timing --- %s seconds ---" % (time.time() - start_time))
    return line_array_dic_map
the string you try to split has less characters than expected 275
>>> sum((2, 10, 24,10,20,20,30,20,20,10,20,89))
275
ok thanks great so i need to ensure each line i split has atleast 275 bytes .....

Hmm Cool didnt think why it was giving such wierd issue
Check str.ljust() method