0% found this document useful (0 votes)
14 views

Topic_4_Read_and_Write_Load_Modules

The document covers essential concepts in Python programming, focusing on importing modules, reading and writing files, and using dictionaries. It explains how to import specific functions from packages, the importance of file modes for reading and writing, and how to manipulate dictionaries with various methods. Additionally, it touches on advanced features like context management for file handling and alternatives for data manipulation using libraries like pandas.

Uploaded by

James SI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Topic_4_Read_and_Write_Load_Modules

The document covers essential concepts in Python programming, focusing on importing modules, reading and writing files, and using dictionaries. It explains how to import specific functions from packages, the importance of file modes for reading and writing, and how to manipulate dictionaries with various methods. Additionally, it touches on advanced features like context management for file handling and alternatives for data manipulation using libraries like pandas.

Uploaded by

James SI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Topic 4: Import

Dictionaries Modules, Read & Write,


Python Programming for Economics
Dr. Marcus Roel
Spring 2024, UNNC

Today
Loading Modules
Read and store data to .txt and .csv files
Dictionaries
Packages and import
By default, Python only has access to a small number of built-in types and functions
Majority of functions are located in packages or modules
To access a function, the package which contains the function must be imported
with keyword import
All import statements should be done at the top of your python file
from package import name_func : imports only a single function
Great when you only need one function
readbility: those functions are clearly listed in the import statement at top of the
file)
In [4]: from numpy import cos
print(cos(0))

1.0

In [5]: from math import sqrt


print(sqrt(4))
print(4**0.5)

2.0
2.0
numpy package adds support for large, multi-dimensional arrays and matrices and
large collection of high-level mathematical functions to operate on these arrays
For non-anaconda users, it must be installed first (see below)
from package import ⁎ imports all functions in package
This import statement should be avoided in most cases
don't know where function is from (if you import more than 1 package)
name conflicts: different packages may have the same named-functions
resulting in one being hidden by another
In [6]: from numpy import *
# All functions in the numpy package can be used directly
# Example: cosine function
print(cos(0))

1.0

Import whole module in one one of two ways:


import numpy
Allows any function to be accessed via numpy.func_name

import numpy as np
Allows any function to be accessed via np.func_name
Standard way most people import; avoids having to write out numpy.
Still obvious where functions come from
In [7]: import numpy
numpy.cos(0) # Functions in numpy are acessed using dot-notation

Out[7]: 1.0

In [8]: # import and rename module using `as`


import numpy as np
np.cos(0) # Functions in numpy are accessed using the renamed np

Out[8]: 1.0

Another very useful package: random()


Module for generating random variables
In [9]: import random

x = random.randint(0,10)

print(f"x = {x}") # Try and excecute cell multiple times.


x = 3

randint(start, stop) returns an integer from the specified range, including(!) the stop
Non-built in Packages
You can also install many other open-source
Be careful installing (shady) "stuff from the internet"!
Step 1: pip3 install packagename
Step 2: import packagename
How do you know all these???
I am older than I look
Google/Stackexchange are your friends

Loading and Saving to Files


Python can open files directly using open(file, mode="rt")
file simply specifies the file either as absolute path or relatively to your .py file

mode specifies what you want to do with the opened file


'r' open for reading (default)
'w' open for writing, truncating the file first
'a' open for writing, appending to the end of file if it exists (creates new if
doesn't exist)
'x' open for exclusive creation, failing if the file already exists
'b' binary mode
't' text mode (default)
'+' open for updating (reading and writing)`
In [10]: f = open("files/topic_4_1.txt") # Open file in read-text;
# Windows users need to use "\" instead for sub-folders.
print(type(f))
f.close() # Close file when you're done like this.

<class '_io.TextIOWrapper'>

For this to work: Save any .txt file of your liking to (a) either the same directly as this
.py file or (b) add a subdirectory like I did relative to this .py file.
Best practise is to use name.close() to close file / empty memory. `
Reading functions
read(): reads whole text
readline(): reads next line. Can be called multiple times.
readlines(): reads all lines as list
all functions only work once per file. When the "book is read" you have to open()
it again
Keep them in the same notebook cell as open() to not end up with surprising
outputs
read()
In [11]: # Use one of the two:
absolute_path = "/Users/m_roel/ [ folder omitted ] /Python Programming for E
relative_path = "files/topic_4_1.txt"
f = open(absolute_path)

text = f.read() # This object has reading ability!


print(f"\nUsing read() yields a {type(text)}")
print(f"\nIt reads the whole text:\n")
print(text)

---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
Cell In[11], line 4
2 absolute_path = "/Users/m_roel/ [ folder omitted ] /Python Programmi
ng for Econ/files/topic_4_1.txt"
3 relative_path = "files/topic_4_1.txt"
----> 4 f = open(absolute_path)
6 text = f.read() # This object has reading ability!
7 print(f"\nUsing read() yields a {type(text)}")

File /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-
packages/IPython/core/interactiveshell.py:308, in _modified_open(file, *arg
s, **kwargs)
301 if file in {0, 1, 2}:
302 raise ValueError(
303 f"IPython won't let you open fd={file} by default "
304 "as it is likely to crash IPython. If you know what you are
doing, "
305 "you can use builtins' open."
306 )
--> 308 return io_open(file, *args, **kwargs)

FileNotFoundError: [Errno 2] No such file or directory: '/Users/m_roel/ [ fo


lder omitted ] /Python Programming for Econ/files/topic_4_1.txt'

readline()
read line by line
In [ ]: f = open("files/topic_4_1.txt")

text = f.readline() # Read first line


print(text)
text = f.readline() # Read next line
print(text)

# My file only has two lines.


# What happens if you call it again?
text = f.readline() # Empty string -> empty line
print(text)
print("Closing file now.")
f.close()

readlines()
reads file as list of lines (with \n at the end of each)
to get rid of the right \n use str.rstrip(), which not only removes whitespace but
also \n
alternatively, just specify what to remove directly: str.rstrip('\n')
In [ ]: f = open("files/topic_4_1.txt")
lines = f.readlines()
f.close() # Data is already saved to variable. f can be closed.
print("Initially read in lines:", lines)

In [ ]: lines_clean = []
for line in lines:
lines_clean.append(line.rstrip())

print("Lines after cleaning:", lines_clean)

In [ ]: # Manual way to get rid of /n


f = open("files/topic_4_1.txt")
lines = f.readlines()
f.close()

# print(lines) # We are here.

lines_clean = []

for line in lines:


# cleaned_text = line[0:len(line)-1]
cleaned_text = line[:-1]
lines_clean.append(cleaned_text)

print(lines_clean)

Implicit reading feature : )


You can also interate over the lines that are part of the file directly
Files that are opened (to read) automatically implement an iterable that allows us to
go over them
Intuitive or not? I am not sure : ).
In [ ]: f = open("files/topic_4_1.txt")
for line in f:
print(line)

f.close()

Writing functions
write(): write text to file (either in append/write mode)
writelines(): writes list of text to file
Numbers must be converted to string to be written.
Writing in appending mode ('a').
In [ ]: f = open("files/topic_4_2.txt", 'r') # Let's read original content
print(f"Content of file is: \n {f.read()} \nContent ended.")
f.close()

In [ ]: f = open("files/topic_4_2.txt", 'a') # Open with append 'a'


text_to_add = " A very meaningful statement here." # Try without space at b
f.write(text_to_add) # Adding to file done via write().
f.close()

In [ ]: f = open("files/topic_4_2.txt", "r") # Open and read the file again


print(f"\nContent of file after editing is:\n{f.read()}")
f.close()

Writing in write mode ('w')


In [ ]: f = open("files/topic_4_2.txt", "w")
f.write("Hehe, all that nonsense is gone now.")
f.close()

In [ ]: f = open("files/topic_4_2.txt", "r") # Read again after overwriting


print(f.read())
f.close()

Other useful modules when writing/reading


the os module: import os
Create a directory (i.e., folder): os.mkdir(folderpath)
Check if file exists: os.path.exists(file)
Delete a file: os.remove(file) (needless to say, be careful with this one)
Remove directory: os.rmdir(folderpath) (folder must be empty)
os.path.join(path, path-or-file): combines folderpath and path according to your
os, i.e., "/" or "\".

Dictionaries (dict)
Dictionaries map keys to values
think language dictionary: key is the word you look up, the value is the
translation/explanation
Created using curly brackets, with commas separating entries: dict_1 =
{key_1: value_1, key_2: value_2}
Values are accessed using keys (instead of relying on an numbered index as in lists)
keys must be unique (immutable) data types
e.g., strings, integers, or tuples (containing immutable types)
values can contain any valid Python data type
Dictionaries are mutable
In [ ]: data = {'name': "Marcus",
'favorite fruit': 'xigua',
666: 'juggle 5 balls'}

# Nicely spaced across lines. Can also be done in a single line.

print(type(data))
# print(data)

In [ ]: # Access data
print(f"Name is {data['name']}")
print(f"... and he can {data[666]}.") # Also, what a show-off!

Edit entries
edit entries (just like lists) by re-assigning a value to an existing key (instead of
location)
In [ ]: data['name'] = "His Highness, the Prince of Genovia"
print(f"His name is now: {data['name']}")

Add new entries


add to existing dictionaries by assigning a value to a non-existing key
In [ ]: import pprint # Nicer way to print "messier" objects.
data['weather today'] = 'A Beautiful Sunny Day'
pprint.pprint(data)

Delete entries
Key-value pairs can be deleted using the reserved keyword del.
In [ ]: del data['favorite fruit']
pprint.pprint(data)

Useful dictionary methods


keys and values methods can be used to retrieve all keys and values of a
dictionary.
the return object is a special built-in object (dict_keys or dict_values)
... which allows us to loop over the items using for loops
to turn into lists, apply list()
items method gives the dictionary's key-value pairs.

In [ ]: print(f"keys in dict data are:\n{data.keys()}")

In [ ]: # The return value is not a list. To see this directly:


print(type(data.keys()))

In [ ]: print(f"values in dict data are:\n{data.values()}\n")

"Typical" application of .items()


In [ ]: for key, value in data.items():
print(key, ":", value)

Checking
container if something is part of a list(like) object / data
to check if something is part of something else, use bolean statement x in
object
example: 'name' in data.keys()

In [ ]: if 'name' in data.keys():
print(f"name = {data['name']}")

if 'UNNC' in data.keys():
print(f"UNNC = {data['UNNC']}")
else:
print(f"UNNC is not a valid key")
To see what happens if we try to access a non-valid key, try
print(data['UNNC'])

General use(fulness) of dicts


Main benefit of dicts is that data is identified by a simple key, that is easily readible.
No need to remember what is stored at which position (which will create nightmares
if things are a little bit more complex)
A lot of data (e.g., APIs) essentially gets passed around using dictionaries (like
objects).
Less common in the more trivial coding stuff such as scientific computing/econ

Appendix
Advanced Features, Code and Use-Cases (non-examinable)
Advanced Syntax: block commands
Use the block command to automatically close the file but retain access to text.
In [ ]: with open("files/topic_4_1.txt") as f:
text = f.read() # Swap with any other (read) command.
print(text) # Text object can be used outside of block-code!

with tag implements context code.


in the case of open() , this means closing the file
This is what you'll find in most code examples online
Alternatives for reading/writing
pandas : .read_excel(), .read_csv(), .to_csv(), .to_excel
very convenient (probably most people's go-to solution for loading data)
More general: jason (pickle)
Bonus: creating dictionaries from two lists
In [ ]: keys = ['a', 'b', 'c']
values = [1, 2, 3]
dictionary = dict(zip(keys, values))
print(dictionary)

You might also like