Mittwoch, 21. Oktober 2015

Project 1: Some Wrangling Code

Use on your local machine!


Example 1)

import csv
import os

def fix_turnstile_data(filenames):
    #filenames is supposed to be a list of filenames
    '''
    Filenames is a list of MTA Subway turnstile text files. A link
    to an example MTA Subway turnstile text file can be seen at the URL below:
    http://web.mta.info/developers/data/nyct/turnstile/turnstile_110507.txt
    
    As you can see, there are numerous data points included in each row of the
    a MTA Subway turnstile text file. 

    You want to write a function that will update each row in the text
    file so there is only one entry per row. A few examples below:
    A002,R051,02-00-00,05-28-11,00:00:00,REGULAR,003178521,001100739
    A002,R051,02-00-00,05-28-11,04:00:00,REGULAR,003178541,001100746
    A002,R051,02-00-00,05-28-11,08:00:00,REGULAR,003178559,001100775
    
    Write the updates to a different text file in the format of
    "updated_" + filename.
    For example:
        1) if you read in a text file called "turnstile_110521.txt"
        2) you should write the updated data to "updated_turnstile_110521.txt"

    The order of the fields should be preserved. 
    
    You can see a sample of the turnstile text file that's passed into this function
    and the the corresponding updated file in the links below:
    
    Sample input file:
    https://www.dropbox.com/s/mpin5zv4hgrx244/turnstile_110528.txt
    Sample updated file:
    https://www.dropbox.com/s/074xbgio4c39b7h/solution_turnstile_110528.txt
    '''
    for name in filenames:
        outgoing_filename = "alternative_updated_" + name
        path = r' ... select your path to input and output files ...'
        g = open(os.path.join(path, outgoing_filename), 'w')
        outgoing_file = csv.writer(g)
   
        with open(os.path.join(path, name), 'rb') as f:
            reader = csv.reader(f)
            for row in reader:
                i = 0
                initial = row[:3]

                while row[3+i:3+i+5]:
                    outgoing_file.writerow(initial + row[3+i:3+i+5])
                    i = i + 5
                   
filenames = ['turnstile_110528.txt', 'turnstile_110529.txt']
fix_turnstile_data(filenames)




Example 2:

# -*- coding: utf-8 -*-
import os
import csv
import re

def create_master_turnstile_file(filenames, output_file):
    '''
    Write a function that takes the files in the list filenames, which all have the 
    columns 'C/A, UNIT, SCP, DATEn, TIMEn, DESCn, ENTRIESn, EXITSn', and consolidates
    them into one file located at output_file.  There should be ONE row with the column
    headers, located at the top of the file.
    
    For example, if file_1 has:
    'C/A, UNIT, SCP, DATEn, TIMEn, DESCn, ENTRIESn, EXITSn'
    line 1 ...
    line 2 ...
    
    and another file, file_2 has:
    'C/A, UNIT, SCP, DATEn, TIMEn, DESCn, ENTRIESn, EXITSn'
    line 3 ...
    line 4 ...
    line 5 ...
    
    We need to combine file_1 and file_2 into a master_file like below:
     'C/A, UNIT, SCP, DATEn, TIMEn, DESCn, ENTRIESn, EXITSn'
    line 1 ...
    line 2 ...
    line 3 ...
    line 4 ...
    line 5 ...
    '''
    path = r' ...path to location for input and output files ...'
     
    with open(os.path.join(path, output_file), 'w') as master_file:
        master_file.write('C/A,UNIT,SCP,DATEn,TIMEn,DESCn,ENTRIESn,EXITSn\n')
        for filename in filenames:
           master_file.write(re.sub('C/A, UNIT, SCP, DATEn, TIMEn, DESCn, ENTRIESn, EXITSn', '',            ''.join(open(os.path.join(path, filename), "rb").readlines())))

create_master_turnstile_file([ 'file_2.txt', 'file_3.txt'], 'output_file.txt')




Keine Kommentare:

Kommentar veröffentlichen