初级
实现数据清洗管道
未完成
初级参考
完整示例代码供参考,建议自己理解后重新输入
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'}
👑
升级 VIP
解锁全部题目,畅通无阻地学习
- ✓ 解锁全部训练包所有题目
- ✓ 查看完整参考代码和提示
- ✓ 浏览器内直接运行 Python 代码
- ✓ 自动批改 + 进度追踪
30天
¥18
1年
¥99
2年
¥158
3年
¥199