# The file is closed automatically just because continuing detended. Of course, open your file with automatic closing after dedent: with open('input.csv', 'rb‘) as csvfile: Read all lines: data = ] for row in reader]ĭata.append(]) So this is shorter: reader = csv.reader(csvfile) You should use delimiter=',': reader = csv.reader(csvfile, dialect='excel', delimiter=',', quotechar='|')ĭialect='excel', delimiter=',' are defaults and quotechar='|' is not need for your example file (keep it if needed). Is there's a more 'pythonic' way of writing this ? ![]() The ternary condition could be written as ord(row)/88, but would it be possible to map the each string row to an integer row of ones and zeros? If(rowCount > 2): #skip first 3 rows (2 empty and 1 label)įor i in xrange(1,len(row)):#skip 1st label columnĭataRow.append(1 if row = 'X' else 0) #append 1s for X, 0s otherwise Reader = csv.reader(csvfile,dialect='excel', delimiter=' ', quotechar='|') I need to parse it into a 2D array containing 1-s where X are and 0 otherwise, ignoring the headers/extra rows.Īfter reading the docs on the csv module I wrote a simple script like so: import csv
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |