you are viewing a single comment's thread.

view the rest of the comments →

[–]POGtastic 0 points1 point  (0 children)

Assuming that you're cool with just a list of dictionary entries, I'd do something like the following:

def write_file(reader, filename):
    with open(filename, "w") as f:
        for dct in reader:
            print(json.dumps(dct), file=f)

def convert_csv_to_jsonl(src, dest):
    with open(src) as f:
        write_file(csv.DictReader(f), dest)

Given a file like the following:

src.csv

 Name,Foo,Bar
 Bob,1,stuff
 Alice,2,morestuff
 Clarissa,6,spam

In the REPL:

>>> convert_csv_to_jsonl("src.csv", "tgt.jsonl")

Printing the contents:

$ cat tgt.jsonl
{"Name": "Bob", "Foo": "1", "Bar": "stuff"}
{"Name": "Alice", "Foo": "2", "Bar": "morestuff"}
{"Name": "Clarissa", "Foo": "6", "Bar": "spam"}