From 967fc721eb8fca776127125d72322ec7cb1f119d Mon Sep 17 00:00:00 2001 From: johcarter Date: Wed, 4 Dec 2024 16:02:08 +0000 Subject: [PATCH] Change quoting of csv input --- utils/quote_change.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 utils/quote_change.py diff --git a/utils/quote_change.py b/utils/quote_change.py new file mode 100644 index 00000000..92b67310 --- /dev/null +++ b/utils/quote_change.py @@ -0,0 +1,31 @@ +import pandas as pd +import csv +import argparse + +parser = argparse.ArgumentParser() +parser.add_argument('-i', '--fin', help='path to the input file') +parser.add_argument('-o', '--fout', help="path to the output file, use input file path if not provided", nargs='?') +parser.add_argument('-q', '--quoting', help='quoting style of the output [all, minimal, ...] (not case sensitive) ' + 'see list in https://docs.python.org/3/library/csv.html#csv.QUOTE_ALL', default='all') + +def to_quote(*, fin, fout=None, quoting=csv.QUOTE_ALL): + if fout is None: + fout = fin + df = pd.read_csv(fin, dtype=str, keep_default_na=False, na_values=[], encoding='latin_1') + df.to_csv(fout, index=False, quoting=quoting) + +def main(): + kwargs = vars(parser.parse_args()) + quoting = getattr(csv, f"QUOTE_{kwargs.pop('quoting').upper()}") + + to_quote(quoting=quoting, **kwargs) + +if __name__ == '__main__': + main() + + + + + + +