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

Dealing with NaN in json files #25

Open
lennertcl opened this issue Nov 8, 2023 · 1 comment
Open

Dealing with NaN in json files #25

lennertcl opened this issue Nov 8, 2023 · 1 comment

Comments

@lennertcl
Copy link

I am trying to read a file which contains data with NaN values, e.g. {"a": NaN}. I know this is not valid JSON, however I would like to know if there is a workaround that allows me to read this file anyway without getting this error:

Exception: Unexpected bytes! Value 'N' Position 170

Should I just replace all occurrences of NaN with null or "NaN" first or is there a better way to handle this?

@lennertcl
Copy link
Author

For reference, I managed to change the file using this code:

with open("original_file.json", "rb") as f:
    with open("updated_file.json", "wb") as f_out:
        while (byte := f.read(1)):
            if byte == b":":
                val = f.read(4)
                if val == b" NaN":
                    f_out.write(b': "NaN"')
                else: 
                    f_out.write(byte)
                    f_out.write(val)
            else:
                f_out.write(byte)

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

1 participant