Python Data Products - University of California, San Diego
[Pages:13]Python Data Products
Course 1: Basics
Lecture: Reading CSV and JSON into Python
Learning objectives
In this lecture we will... ? Demonstrate the main methods to read CSV/TSV
and JSON files in Python ? Understand some of the edge cases that make
reading these formats difficult
Python Data Products Specialization: Course 1: Basic Data Processing...
CSV/TSV in Python
In this lecture we'll look through a few functions to read CSV/TSV and JSON
data in Python:
? string.split() ? csv.reader (library) ? eval() and ast.eval() ? json.loads (library)
Python Data Products Specialization: Course 1: Basic Data Processing...
Code: String.split()
Note: preserves whitespace!
? Converts a string to a list, given a separator ? By default, any whitespace separator is used (tab, space,
newline) ? But different separators can be provided via an optional
argument
Python Data Products Specialization: Course 1: Basic Data Processing...
Code: String.split()
What happens when the delimiter appears in the column?
Note: splits into three columns rather than two!
? This could be addressed by using a different delimiter (e.g. ';'), though this doesn't generalize for fields containing arbitrary text
? Normally, the field will be escaped by quotes
Python Data Products Specialization: Course 1: Basic Data Processing...
Code: CSV.reader
Note: specify what delimiter to use (tab) first line is the header
Python Data Products Specialization: Course 1: Basic Data Processing...
Code: CSV.reader
next line is the first review in the dataset
Python Data Products Specialization: Course 1: Basic Data Processing...
Code: eval()
Reading json files is even easier as they're very similar to Python's built-in dictionaries:
Note: first line of Yelp's review data
Python Data Products Specialization: Course 1: Basic Data Processing...
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related searches
- university high school san diego ca
- university of california essay prompts
- university of california supplemental essays
- university of california free tuition
- university of california campuses
- university of california online certificates
- preliminary change of ownership san diego ca
- address university of california irvine
- university of california at irvine ca
- university of california irvine related people
- university of california irvine staff
- university of california irvine employment