Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: 'bytes' object has no attribute 'encode' #1140

Open
MeYa2111 opened this issue Jan 28, 2025 · 5 comments
Open

AttributeError: 'bytes' object has no attribute 'encode' #1140

MeYa2111 opened this issue Jan 28, 2025 · 5 comments

Comments

@MeYa2111
Copy link

MeYa2111 commented Jan 28, 2025

Hello,

I don´t know if this issue has been fixed with newer versions and I´m not the best when it comes to coding, however when trying to save mdf_safe.save() it will sometimes throw you the error code:

\Python\Python39\site-packages\asammdf\blocks\v4_blocks.py", line 6332, in to_blocks
text=text.encode("utf-8", "replace"),
AttributeError: 'bytes' object has no attribute 'encode'

I dont know if its just human Error of mine, but I found easy fix by changing that to_blocks() function with :
if text: if text in defined_texts: self.name_addr = defined_texts[text] else: if isinstance(text, bytes): text = text.decode("utf-8", "replace") text = text.encode("utf-8", "replace") tx_block = TextBlock( text = text, meta=False, safe=True, ) self.name_addr = address defined_texts[text] = address tx_block.address = address address += tx_block.block_len blocks.append(tx_block) else: self.name_addr = 0
or by simply replacing the whole function with

`
def to_blocks(
self,
address: int,
blocks: list[Any],
defined_texts: dict[str, int],
si_map: dict[bytes, int],
) -> int:
id_ = id(self)
if id_ in si_map:
return address

    text = self.name
    if text:
        if text in defined_texts:
            self.name_addr = defined_texts[text]
        else:
            if isinstance(text, bytes):
                text = text.decode("utf-8", "replace")
            text = text.encode("utf-8", "replace")
            tx_block = TextBlock(
                text = text,
                meta=False,
                safe=True,
            )
            self.name_addr = address
            defined_texts[text] = address
            tx_block.address = address
            address += tx_block.block_len
            blocks.append(tx_block)
    else:
        self.name_addr = 0

    text = self.path
    if text:
        if text in defined_texts:
            self.path_addr = defined_texts[text]
        else:
            tx_block = TextBlock(
                text=text.encode("utf-8", "replace"),
                meta=False,
                safe=True,
            )
            self.path_addr = address
            defined_texts[text] = address
            tx_block.address = address
            address += tx_block.block_len
            blocks.append(tx_block)
    else:
        self.path_addr = 0

    text = self.comment
    if text:
        if text in defined_texts:
            self.comment_addr = defined_texts[text]
        else:
            meta = text.startswith("<SI")
            tx_block = TextBlock(
                text=text.encode("utf-8", "replace"),
                meta=meta,
                safe=True,
            )
            self.comment_addr = address
            defined_texts[text] = address
            tx_block.address = address
            address += tx_block.block_len
            blocks.append(tx_block)
    else:
        self.comment_addr = 0

    bts = bytes(self)
    if bts in si_map:
        self.address = si_map[bts]
    else:
        blocks.append(bts)
        si_map[bts] = si_map[id(self)] = address
        self.address = address
        address += self.block_len

    return address

`

I hope this isn´t old news. Sorry if something is wrong its my first issue post on Github

@danielhrisca
Copy link
Owner

are you editing some attributes before saving the file?

@MeYa2111
Copy link
Author

MeYa2111 commented Jan 28, 2025

What I do is I go through multiple mf4 files and just looking for the signals I need by name. The only attribute I edit of the mf4 file is the start_time as I need them to be concaded into one at the end and it's important that the timestamps line up. Here the code before the saving for ease of understanding

`
def search_and_safe(mdf_read, channel_names):
mdf_safe = MDF()
channel_signal_group = []

    for read_group_index, read_group in tqdm(enumerate(mdf_read.groups)):
        for read_channel_index, read_channel in enumerate(read_group.channels):
            if read_channel.name in channel_names:
                read_signal = mdf_read.get(read_channel.name, group=read_group_index, raw=True)
                channel_signal_group.append(read_signal)
        
    for group in channel_signal_group:
        mdf_safe.append(group)
    
    mdf_safe.start_time = mdf_read.start_time

    return mdf_safe

`

@danielhrisca
Copy link
Owner

which asammdf version do you use?

@MeYa2111
Copy link
Author

MeYa2111 commented Feb 6, 2025

I use version 8.0.1

@danielhrisca
Copy link
Owner

@MeYa2111 please try the development branch code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants