Suppose there is a CSV table with row and column headings, for example:
, "Car", "Bike", "Boat", "Plane", "Shuttle" "Red", 1, 7, 3, 0, 0 "Green", 5, 0, 0, 0, 0 "Blue", 1, 1, 4, 0, 1
I want to get row and column headers, i.e.:
col_headers = ["Car", "Bike", "Boat", "Plane", "Shuttle"] row_headers = ["Red", "Green", "Blue"] data = [[1, 7, 3, 0, 0], [5, 0, 0, 0, 0], [1, 1, 4, 0, 1]]
Of course I can do something like
import csv with open("path/to/file.csv", "r") as f: csvraw = list(csv.reader(f)) col_headers = csvraw[1][1:] row_headers = [row[0] for row in csvraw[1:]] data = [row[1:] for row in csvraw[1:]]
... but he doesn't look Pythonic enough.
Is there a cleaner way for this natural operation?