Skip to content

Instantly share code, notes, and snippets.

@christophergregory
Last active August 29, 2015 14:05
Show Gist options
  • Save christophergregory/2e9ed1e97329e427faa5 to your computer and use it in GitHub Desktop.
Save christophergregory/2e9ed1e97329e427faa5 to your computer and use it in GitHub Desktop.
Class 2 Problem - Combine files into one CSV file
"""
Problem: You are given two files, one contains a list of
names (names.txt) and the other a list of phone numbers (numbers.txt).
Your boss needs a single CSV file that can be loaded into excel.
Your task is to combine both txt files into one CSV file.
The first and last names need to be in separate columns
and the phone number must be in the format (555)555-5555
One row in the csv file might look like this:
Smith,John,(555)555-5555
"""
names = [] # a blank list to hold list of names
# Open names.txt as "Readable"
with open('names.txt', 'r') as namesfile:
for line in namesfile: # iterate through each line in names file
names.append(line.rstrip()) # append line to names list
# Open numbers.txt as "Readable"
with open('numbers.txt', 'r') as numbersfile:
# a more "pythonic" way to accomplish the same thing as above
# is to use "list comprehensions" read more here
# https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions
# notice that we took 3 lines and cut them down to 1
numbers = [line.rstrip() for line in numbersfile]
# Creates a new file "output.csv" that you can write
with open('output.csv', 'w') as csvfile:
for index, name in enumerate(names):
first_name = name.split(" ")[0] # split name into two words and grab the first one
last_name = name.split(" ")[1] # split name into two words and grab the second one
number = numbers[index].split(".") # split number into three section at each "."
# format a string in the correct order
# note that you CAN separate the arguments into separate lines
# for legability see http://legacy.python.org/dev/peps/pep-0008/
# don't for get your "\n" character to create a new line!
formatted_line = "{},{},({}){}-{}\n".format(last_name,
first_name,
number[0],
number[1],
number[2])
csvfile.write(formatted_line)
Dudley Hobgood
Tempie Micco
Malka Muse
Mark Goodridge
Bertha Verrill
Shanelle Tolentino
Conchita Coggins
Korey Rossiter
Ilana Minelli
Tennie Buenrostro
Porsche Grear
Risa Mcallister
Yan Bazan
Nia Colquitt
Ludie Harr
Chia Trusty
Iona Blossom
Jovita Albritton
Trish Wyatt
Maggie Tworek
Armida Galles
Raymonde Fulton
Wes Stradford
Karan Levron
Chang Sites
Jeanetta Laber
Hellen Rippe
Veronika Manna
Isaac Nakagawa
Steffanie Bodie
246.815.6506
459.914.2531
283.885.1342
759.523.2223
717.449.8531
536.742.1988
220.801.6527
520.822.2719
924.153.9583
750.621.3208
605.684.6700
720.105.6690
634.819.5217
720.807.2236
894.543.8582
415.406.5023
491.339.6329
224.368.2485
236.432.3196
524.230.3597
518.435.7734
102.601.7860
267.900.9070
577.333.8723
421.874.5125
511.206.3717
620.740.8837
318.117.9435
279.436.7809
629.636.2834
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment