You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

技术需求:在outcome目录创建指定格式的hw_diamonds.csv(禁用Pandas)

生成指定格式的hw_diamonds.csv文件

核心要求

  • 在outcome目录下创建文件hw_diamonds.csv
  • 文件首行固定列名:carat、cut、color、clarity、depth、table、price
  • 后续行填充数据集df对应列的数据
  • 列分隔符使用斜杠(/),行分隔符为换行符
  • 禁止使用Pandas库完成此操作

示例文件(前3行)

carat/cut/color/clarity/depth/table/price
0.23/Ideal/E/SI2/61.5/55.0/326
0.21/Premium/E/SI1/59.8/61.0/326

参考实现代码

with open("outcome/hw_diamonds.csv", "w") as fw:
    # 写入表头
    header = "carat/cut/color/clarity/depth/table/price\n"
    fw.write(header)
    print(header, end="")

    # 遍历数据集逐行写入
    for idx, row in df.iterrows():
        carat = row.carat
        cut = row.cut
        color = row.color
        clarity = row.clarity
        depth = row.depth
        table = row.table
        price = row.price
    
        line = "{}/{}/{}/{}/{}/{}/{}\n".format(carat, cut, color, clarity, depth, table, price)
        fw.write(line)
        print(line, end="") 

内容的提问来源于stack exchange,提问作者hotmama909

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 21:37:21