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

Encoding is variably UTF-8 or ISO-8859-1 #8

Open
dhdaines opened this issue Jan 5, 2024 · 1 comment
Open

Encoding is variably UTF-8 or ISO-8859-1 #8

dhdaines opened this issue Jan 5, 2024 · 1 comment

Comments

@dhdaines
Copy link

dhdaines commented Jan 5, 2024

Hi! Great work on this dataset - unfortunately, reading the files with Python fails because the encodings vary in some of the records. Luckily they all seem to be either UTF-8 or ISO-8859-1 for the moment. You can fix them with this script:

#!/usr/bin/env python3

import chardet
import logging
from pathlib import Path

for path in Path(".").glob("*.csv"):
    tmp = path.with_suffix(".csv.tmp")
    with open(path, "rb") as infh, open(tmp, "wt") as outfh:
        for idx, spam in enumerate(infh):
            try:
                line = spam.decode("utf8")
            except UnicodeDecodeError:
                det = chardet.detect(spam)
                logging.warning(
                    "Line %d of %s is not UTF8, probably %s, re-encoding it",
                    idx,
                    path,
                    det["encoding"],
                )
                line = spam.decode(det["encoding"])
            outfh.write(line)
    tmp.rename(path)
@nonviolent-action-lab
Copy link
Owner

Apologies for the hassle, and thank you for posting this workaround.

In a recent R update, the way encoding gets handled was changed. I think that's why this is happening now, but I haven't been able to dig into it yet. I hope I can get this sorted in the near future.

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