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
  encode/decode to show correct country letters in a CTk combobox janeik 2 656 Sep-02-2023, 09:46 AM
Last Post: janeik
  doing string split with 2 or more split characters Skaperen 22 2,317 Aug-13-2023, 01:57 AM
Last Post: Skaperen
Sad How to split a String from Text Input into 40 char chunks? lastyle 7 1,054 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 4,599 Apr-24-2023, 03:40 PM
Last Post: ctrldan
  [split] Parse Nested JSON String in Python mmm07 4 1,412 Mar-28-2023, 06:07 PM
Last Post: snippsat
  Split string using variable found in a list japo85 2 1,235 Jul-11-2022, 08:52 AM
Last Post: japo85
  Convert string to float problem vasik006 8 3,269 Jun-03-2022, 06:41 PM
Last Post: deanhystad
  UnicodeEncodeError: 'ascii' codec can't encode character '\xfd' in position 14: ordin Armandito 6 2,641 Apr-29-2022, 12:36 PM
Last Post: Armandito
  Split string knob 2 1,839 Nov-19-2021, 10:27 AM
Last Post: ghoul
  f string concatenation problem growSeb 3 2,212 Jun-28-2021, 05:00 AM
Last Post: buran

Forum Jump:

User Panel Messages

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