社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

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

NMOLL • 3 年前 • 1156 次点击  

获取无效语法,但不确定原因。请参阅包含以下代码的行: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
                           
   
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/132192
 
1156 次点击  
文章 [ 1 ]  |  最新文章 3 年前
XxJames07-
Reply   •   1 楼
XxJames07-    3 年前

括号不是闭合的

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

而是写:

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