Let's have a CSV table with row and column headers, e.g.:
, "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 it does not look Pythonic enough.
Is there a neater way for this natural operation?
Step 1: In order to read rows in Python, First, we need to load the CSV file in one object. So to load the csv file into an object use open() method. Step 2: Create a reader object by passing the above-created file object to the reader function. Step 3: Use for loop on reader object to get each row.
Take a look at csv.DictReader
.
If the fieldnames parameter is omitted, the values in the first row of the csvfile will be used as the fieldnames.
Then you can just do reader.fieldnames
. This, of course, only gives you column headers. You would still have to parse the row headers manually.
I think your original solution is pretty good, however.
Now I see that what I want is the easiest (and the most robust) to accomplish with Pandas.
import pandas as pd
df = pd.read_csv('foo.csv', index_col=0)
And if I want, it is easy to extract:
col_headers = list(df.columns)
row_headers = list(df.index)
Otherwise, in the "raw" Python, it seems that the method I wrote in the question is "good enough".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With