Created
September 7, 2012 00:13
-
-
Save pythonjunkie/3661731 to your computer and use it in GitHub Desktop.
Reading a csv file
This file contains 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 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment