Return to Snippet

Revision: 50130
at August 11, 2011 11:11 by ronakpatel2006


Initial Code
import os
import sys
import csv

def opencsv(filename):
    tfile = open(filename, "r")
    line  = tfile.readline()
    tfile.close()
    if line[0] == '"':
        quote_char = '"'
        quote_opt  = csv.QUOTE_ALL
    elif line[0] == "'":
        quote_char = "'"
        quote_opt  = csv.QUOTE_ALL
    else:
        quote_char = '"'
        quote_opt  = csv.QUOTE_MINIMAL
    if   line.find('\t') != -1:
        delim_char = '\t'
    else:
        delim_char = ','
    tfile  = open(filename, "rb")
    reader = csv.reader(tfile, delimiter=delim_char, quotechar=quote_char, quoting=quote_opt)
    return (tfile, reader)

def display():

    ifile = open('/home/rpatel/complete_error_codes.csv', "rb")
    find_code = raw_input("Enter the code: ")
    reader = csv.reader(ifile)

    rownum = 0
    for row in reader:
        if rownum == 0:
            header = row
        else:
            column = 0
            if row[1] == str(find_code):
                for col in row:
                    print'%-8s: %s' % (header[column], col)
                    column += 1
                break
        rownum += 1
    ifile.close()

if __name__ == '__main__':

    try:
        display()
    except Exception, e:
        print("Exception in display: %s" % e)

Initial URL

                                

Initial Description

                                

Initial Title
Read a csv file

Initial Tags
csv

Initial Language
Python