我正在尝试处理以下代码:
ds = load_csv('C:\\User.csv')
f = open(ds,'r')
lines = f.readlines()[1:]
print(lines)
f.close()
数据集的第一行是字符串。我收到以下错误:
TypeError: expected str, bytes or os.PathLike object, not list
虽然当我尝试使用以下代码打开文件时它可以工作:
filename='C:\\User.csv'
f = open(filename,'r')
lines = f.readlines()[1:]
print(lines)
f.close()
我忽略了第一行,因为它的字符串和数据集的其余部分是浮动的。
更新:
加载_csv
def load_csv(ds):
dataset = list()
with open(ds, 'r') as file:
csv_reader = reader(file)
for row in csv_reader:
if not row:
continue
dataset.append(row)
return dataset
即使我使用这种方式仍然会得到错误:
ds = load_csv('C:\\Users.csv')
minmax = dataset_minmax(ds)
normalize_dataset(ds, minmax)
def dataset_minmax(dataset):
minmax = list()
for i in range(len(dataset[0])):
col_values = [row[i] for row in dataset]
value_min = min(col_values)
value_max = max(col_values)
minmax.append([value_min, value_max])
return minmax
def normalize_dataset(dataset, minmax):
for row in dataset:
for i in range(len(row)):
row[i] = (row[i] - minmax[i][0]) / (minmax[i][1] - minmax[i][0])
它给出了以下错误:
row[i] = (row[i] - minmax[i][0]) / (minmax[i][1] - minmax[i][0])
错误:
TypeError: unsupported operand type(s) for -: 'str' and 'str'