From 9b2e909224c1cff42dd382603ece0545ceabf1f5 Mon Sep 17 00:00:00 2001 From: Ad Timmering Date: Sun, 7 Apr 2024 23:36:14 +0900 Subject: [PATCH] fix: add file encoding support to csvreader fixed support for importers on non-UTF8 (e.g. Shift-JIS) text files. Fixes #99. --- beancount_reds_importers/libreader/csvreader.py | 7 +++++-- .../libtransactionbuilder/investments.py | 6 +++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/beancount_reds_importers/libreader/csvreader.py b/beancount_reds_importers/libreader/csvreader.py index 92f8611..0bd11af 100644 --- a/beancount_reds_importers/libreader/csvreader.py +++ b/beancount_reds_importers/libreader/csvreader.py @@ -74,7 +74,10 @@ def initialize_reader(self, file): # print(self.header_identifier, file.head()) def deep_identify(self, file): - return re.match(self.header_identifier, file.head()) + return re.match( + self.header_identifier, + file.head(encoding=getattr(self, "file_encoding", None)), + ) def file_date(self, file): "Get the maximum date from the file." @@ -135,7 +138,7 @@ def convert_date(d): return rdr def read_raw(self, file): - return etl.fromcsv(file.name) + return etl.fromcsv(file.name, encoding=getattr(self, "file_encoding", None)) def skip_until_main_table(self, rdr, col_labels=None): """Skip csv lines until the header line is found.""" diff --git a/beancount_reds_importers/libtransactionbuilder/investments.py b/beancount_reds_importers/libtransactionbuilder/investments.py index 2bab989..d089e06 100644 --- a/beancount_reds_importers/libtransactionbuilder/investments.py +++ b/beancount_reds_importers/libtransactionbuilder/investments.py @@ -258,9 +258,9 @@ def generate_trade_entry(self, ot, file, counter): if "sell" in ot.type: units = -1 * abs(ot.units) if not is_money_market: - metadata[ - "todo" - ] = "TODO: this entry is incomplete until lots are selected (bean-doctor context )" # noqa: E501 + metadata["todo"] = ( + "TODO: this entry is incomplete until lots are selected (bean-doctor context )" # noqa: E501 + ) if ot.type in [ "reinvest" ]: # dividends are booked to commodity_leaf. Eg: Income:Dividends:HOOLI