代码之家  ›  专栏  ›  技术社区  ›  NMOLL

python_数据['division']中的语法无效。追加(int(拆分[3]))

  •  -1
  • NMOLL  · 技术社区  · 3 年前

    获取无效语法,但不确定原因。请参阅包含以下代码的行:data['division']。追加(int(拆分[3]))

    def getBMData(filename):
        """Read the contents of the given file. Assumes the file
        in a comma-seperated format, with 6 elements in each entry:
        0. Name (string), 1. Gender (string), 2. Age (int),
        3. Division (int), 4. Country (string), 5. Overall time (float)
        Returns: dict containing a list for each of the 6 variables."""
        
        data = {}
        f = open(filename)
        line = f.readline()
        data['name'], data['gender'], data['age'] = [], [], []
        data['division'], data['country'], data['time'] = [], [], []
        while line != '':
            split = line.split(',')
            data['name'].append(split[0])
            data['gender'].append(split[1])
            data['age'].append(int(split[2])
            data['division'].append(int(split[3]))
            data['country'].append(split[4])
            data['time'].append(float(split[5][:-1])) #remove \n
            line = f.readline()
        f.close()
        return data
                               
       
    
    1 回复  |  直到 3 年前
        1
  •  0
  •   XxJames07- jitheshKuyyalil    3 年前

    括号不是闭合的

    data["age"].append(int(split[2]) <- here
    

    而是写:

    data["age"].append(int(split[2]))