David Schmudde / Sep 23 2019

Jupyter Basics: Reading Files

Loading data into a Python or a Python library is a common first step for people new to Jupyter notebooks. This is an introduction.

Upload Data to Nextjournal

artist_data.csv

Where did it go? Use !ls magic command to see.

!ls 
artist_data.csv

Move Data to a Project Directory

Make sure you collect all your data in a logical place when creating a Jupyter project. Use the %%bash magic to use command line operations within a Jupyter cell.

%%bash

mkdir -p /project/artist/
cp /.nextjournal/data-named/QmaVnxKK4DkqiVEG5Puh5bEZxwh7yxsfksSHUPEmUbAXpV/artist_data.csv /project/artist/artist_data.csv
ls /project/artist

Load Into Memory

Opening and Closing the File

There are several switches available to the open() method. For example:

  • r: read mode
  • w: write mode
  • a: append mode
f = open("/project/artist/artist_data.csv","r")
print(f.read())
f.close()

Don't forget to close() the file after opening it.

Opening a File With a Library

import pandas as pd

data = pd.read_csv("/project/artist/artist_data.csv")

data.head(6)