Skip to content

Read text files with utf-8-sig to drop a byte order mark - #670

Open
jayhemnani9910 wants to merge 1 commit into
fabiocaccamo:mainfrom
jayhemnani9910:fix/read-files-utf8-sig
Open

jayhemnani9910 wants to merge 1 commit into
fabiocaccamo:mainfrom
jayhemnani9910:fix/read-files-utf8-sig

Conversation

@jayhemnani9910

Copy link
Copy Markdown

Describe your changes

Files that start with a UTF-8 byte order mark load wrong or not at all. Excel's "CSV UTF-8" option writes one, and so do PowerShell 5 and older Windows editors:

benedict.from_csv("excel.csv")["values"][0]
# {'name': 'Anna', 'city': 'Berlin'}   -> d["name"] is a KeyError
benedict.from_json("bom.json")   # ValueError
benedict.from_ini("bom.ini")     # ValueError
benedict.from_toml("bom.toml")   # ValueError

read_content_from_file now reads text files with encoding="utf-8-sig", which drops a leading BOM and reads files without one exactly as before.

Related issue

None.

Checklist before requesting a review

  • I have performed a self-review of my code.
  • I have added tests for the proposed changes.
  • I have run the tests and there are not errors.

Tests: BOM fixtures for CSV and JSON with two tests; both fail before the change. pytest tests -k "not s3 and not url" passes (934).

A CSV saved by Excel as "CSV UTF-8" starts with a BOM, which ended up in
the first column name ('name'), and JSON, INI and TOML files with a
BOM failed to parse.
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

Successfully merging this pull request may close these issues.

1 participant