← 返回题库
初级

实现数据清洗管道

未完成
初级参考 完整示例代码供参考,建议自己理解后重新输入
def solve():
    from pyodide.http import open_url
    from io import StringIO
    raw_requests_csv = open_url("https://data.zuihe.com/dbd/ms-shop/state_01/raw_requests.csv").read()
    import json
    def strip(val): return val.strip() if isinstance(val, str) else val
    def lowercase(val): return val.lower() if isinstance(val, str) else val
    def trim_zeros(val):
        if isinstance(val, str) and val.replace('.','').lstrip('0').isdigit():
            return str(float(val))
        return val
    def normalize_phone(val):
        if isinstance(val, str): return val.replace('-','').replace(' ','')
        return val
    PIPELINE = {
        'username': [strip, lowercase],
        'email': [strip, lowercase],
        'phone': [strip, normalize_phone],
        'price': [strip, trim_zeros],
    }
    def clean(data):
        result = dict(data)
        for field, fns in PIPELINE.items():
            if field in result:
                for fn in fns: result[field] = fn(result[field])
        return result
    test_data = [
        {'username':'  Alice_New ','email':'ALICE@TEST.COM','phone':'138-1234-5678'},
        {'username':'BOB','email':'  BOB@example.com  ','phone':'139 0000 1111'},
    ]
    for d in test_data:
        cleaned = clean(d)
        print(f"before: {d}")
        print(f"after:  {cleaned}")

示例

输入
solve()
期望输出
before: {'username': '  Alice_New ', 'email': 'ALICE@TEST.COM', 'phone': '138-1234-5678'}
after:  {'username': 'alice_new', 'email': 'alice@test.com', 'phone': '13812345678'}
before: {'username': 'BOB', 'email': '  BOB@example.com  ', 'phone': '139 0000 1111'}
after:  {'username': 'bob', 'email': 'bob@example.com', 'phone': '13900001111'}
Python 代码 🔒 登录后使用
🔒

登录后即可练习

注册免费账号,在浏览器中直接运行 Python 代码