Encode string split problem - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Encode string split problem (/thread-12212.html) |
Encode string split problem - sripylearn - Aug-14-2018 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 RE: Encode string split problem - buran - Aug-14-2018 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 RE: Encode string split problem - sripylearn - Aug-14-2018 ok thanks great so i need to ensure each line i split has atleast 275 bytes ..... Hmm didnt think why it was giving such wierd issue RE: Encode string split problem - buran - Aug-14-2018 Check str.ljust() method |