CSC111 Filtering a CSV File

From dftwiki3
Jump to: navigation, search

--D. Thiebaut (talk) 20:50, 22 November 2015 (EST)



CSV File


  • The CSV file is available here.


Source Code


# filterCSVFile.py
# D. Thiebaut
# A reminder about try/except clauses in Python
# and how to quickly deal with exceptions.

def main():
    # open the CSV File.
    file = open( "/Users/thiebaut/Downloads/SmithMapCSV.txt", "r" )


    # process it, line by line.
    for line in file:
        line = line.strip()

        # skip comments
        if line.startswith( "#" ):
            continue

        # split it and extract the name.
        try:
            fields = line.split( "," )
            buildingName = fields[1]
            print( "building name: ", buildingName )
        
        except IndexError:
            # if we're here, we couldn't split the line
            # just discard it and go grab another one.
            continue


main()