python_data [' difiss']。append(int(split [3]))

发布于 2025-01-22 03:34:39 字数 1036 浏览 3 评论 0原文

获取无效的语法,不确定为什么。请参阅具有以下代码的行:data ['diver']。附加(int(split [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
                           
   

Getting invalid syntax and not sure why. See line that has the following code: data['division'].append(int(split[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
                           
   

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仅冇旳回忆 2025-01-29 03:34:39

括号不是关闭的,

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

而是写下:

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

the parentheses is not closed

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

instead write:

data["age"].append(int(split[2]))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文