wireservice / csvkit

A suite of utilities for converting to and working with CSV, the king of tabular file formats.
https://csvkit.readthedocs.io
MIT License
6.03k stars 603 forks source link

csvjson: question/suggestion about passing in input_file to csvjson as an argument at initialisation [non-cli] #1133

Closed Nuh-h closed 3 years ago

Nuh-h commented 3 years ago

While trying to upgrade a python2 flask app, I discovered csvjson doesn't (to my knowledge) have any direct method/constructor that allows you to submit address of an input filename (input_path) for instance from within a python file rather than a cli.

Does something already exist for CSVJSON to accept input_path at initialisation of the class? If not, then any chance of adding that in? I currently modified csvjson for myself like this.

#csvjson.py
def __init__(self, args=None, output_file=None):
        super(CSVJSON, self).__init__(args, output_file)

        #Added this to ensure there was an input file
        self.input_file = args[0] 

        self.validate_args()

        self.json_kwargs = {
            'ensure_ascii': False,
            'indent': self.args.indent,
        }
        ...
        ...

and from my flask app, I initialise simply as this:

def create_json_from_csv(in_name, out_name):
    #in_name and out_name are both file paths
    args = [in_name]
    out_file = open(out_name, "w", encoding="utf-8")
    CSVJSON(args, out_file).main()
    out_file.close()
jpmckinney commented 3 years ago

It should work without that modification. Just call run() instead of main().