How to read numbers in python from csv file? How to read numbers in python from csv file? numpy numpy

How to read numbers in python from csv file?


Just cast as you append:

 n.append(float(row[8]))

If there are empty strings catch those before appending.

try:    n.append(float(row[8]))except ValueError:   continue

Or you might want to try pandas, in particular pandas.read_csv:

import pandas as pddf = pd.read_csv("in.csv")print(df["col_name"].mean())


Just add quoting:

with open('tab.csv', newline='') as file:    reader = csv.reader(file, quoting=csv.QUOTE_NONNUMERIC)    n=[]    for row in reader:        n.append(row[8])