This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
people = {} # let's start with an empty dictionary to hold our data | |
with open('numbers.txt', 'r') as numbers: | |
for index, line in enumerate(numbers): | |
if index > 0: # skip the first line (don't need the headers) | |
name, birthday = line.rstrip().split(',') | |
# study this line carefully, it's intentionally terse | |
people.update({ name : { 'number' : birthday } }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
# Open all required files with at once | |
with open('numbers.txt', 'r') as numbersfile, \ | |
open('birthdays.txt', 'r') as birthdayfile, \ | |
open('output.txt', 'w') as output: | |
# Read in each line as a dictionary | |
# https://docs.python.org/2/library/csv.html#csv.DictReader | |
numReader = csv.DictReader(numbersfile, fieldnames=['Name','Number']) |
OlderNewer