From the course: Data Ingestion with Python
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Solution: CSV to JSON - Python Tutorial
From the course: Data Ingestion with Python
Solution: CSV to JSON
(upbeat music) - [Instructor] Here's my solution to the problem. We're importing bz2 since the file is compressed and the csv to process csv. We're importing JSON since we're going to admit JSON. And we're going to import datetime since we're going to do some time conversions. In line 11, I have a function to parse the timestamp for the text. In line 15, I defined the converters from every type in the csv, which is only text to right Python type. And then, in line 26, I have iter records, which will iterate over the lines and will yield one record at a time, which is a dictionary with the correct types. This the part of reading the csv. Now I need to encode it as JSON. Remember, the JSON does not support datetime object, so we need to encode them as strings. What I'm going to do, in line 37, is define encode time, which is going to encode an object, if it's a datetime object, as a string. In line 43, I'm going to use…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.