Importing Flat Files & Other Data Flashcards
Importing Data in Python (Part 1)
how to access the system shell in IPython (on DataCamp)
!
display directory contents
! ls
open a text file as read-only
open(‘file.txt’, ‘r’)
print an open file
print(file.read())
check if a file is closed
file.closed
close a file
file.close()
alternative to opening and closing a file
context manager: with open(‘file.txt’) as file:
read one line of a file
file.readline()
flat files
table data without structural relationships (like a database would have)
packages to import flat files
NumPy or pandas
how to import a flat file with NumPy
np.loadtext(file, delimiter=, skiprows=, usecols=, dtype=
tab delimiter
‘\t’
how to import mixed datatypes with NumPy
np.genfromtxt(file, delimiter=, names=, dtype=None)
names argument
if =True, tells us there is a header
what does genfromtxt() produce
a structured array; 1D array where each element is a row of the flat file imported