目标:读取CSV并将Str字段映射到整数索引 语言:Python 3.x
如果某列不是float-cast-able,请将其唯一值映射到值为索引的Dict (ROW 3 =“帮助”,“我”,“汤姆克鲁斯”,“帮助”) MapRow3 = {帮助:0,我:1,汤姆克鲁斯:2}
当我运行代码时,它总是点击“除外”。我在读取文件时尝试跳过第一行(标签),但这也不起作用。我假设我做错了。
def makePlot(filename):
reader = np.loadtxt(filename, dtype=str, delimiter=',')
reader = reader.T #transpose matrix so columns become rows
for row in reader:
try:
reader[row] = list(float(reader[row])) #it always skips this line
except:
myset = set(row) #I'd rather this be a dict as described above