Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Encode string split problem
#1
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
Reply
#2
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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
Reply
#4
Check str.ljust() method
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] ibm_db install problem SQLPython 1 660 Feb-13-2025, 07:24 PM
Last Post: buran
  [split] another problem with code blakeusherremix68 0 410 Dec-23-2024, 11:36 PM
Last Post: blakeusherremix68
  Unable to understand the function string.split() Hudjefa 8 2,491 Sep-16-2024, 04:25 AM
Last Post: Pedroski55
  encode/decode to show correct country letters in a CTk combobox janeik 2 1,880 Sep-02-2023, 09:46 AM
Last Post: janeik
  doing string split with 2 or more split characters Skaperen 22 6,240 Aug-13-2023, 01:57 AM
Last Post: Skaperen
Sad How to split a String from Text Input into 40 char chunks? lastyle 7 2,631 Aug-01-2023, 09:36 AM
Last Post: Pedroski55
Question UnicodeEncodeError: 'ascii' codec can't encode character u'\xe8' in position 562: ord ctrldan 23 9,565 Apr-24-2023, 03:40 PM
Last Post: ctrldan
  [split] Parse Nested JSON String in Python mmm07 4 2,760 Mar-28-2023, 06:07 PM
Last Post: snippsat
  Split string using variable found in a list japo85 2 2,164 Jul-11-2022, 08:52 AM
Last Post: japo85
  Convert string to float problem vasik006 8 5,401 Jun-03-2022, 06:41 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020