技术需求:在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
相关产品推荐
相关产品推荐

