使用Python获取以下示例的CSV输出 [英] Using Python to get a CSV output for the following example

查看:151
本文介绍了使用Python获取以下示例的CSV输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我再次回到我正在进行的学生项目分配问题的传奇。感谢 Moron (与他的同事不匹配),我有一个方向为我的项目的评估部分。

I'm back again with my ongoing saga of Student-Project Allocation questions. Thanks to Moron (who does not match his namesake) I've got a bit of direction for an evaluation portion of my project.

想到赋值问题和匈牙利算法的想法,我想以.csv文件的形式表达我的数据,这样在电子表格中看起来就像这样形成。这是基于我在此处看到的结构。

Going with the idea of the Assignment Problem and Hungarian Algorithm I would like to express my data in the form of a .csv file which would end up looking like this in spreadsheet form. This is based on the structure I saw here.

|          | Project 1 | Project 2 | Project 3 |
|----------|-----------|-----------|-----------|
|Student1  |           |     2     |     1     |
|----------|-----------|-----------|-----------|
|Student2  |     1     |     2     |     3     |
|----------|-----------|-----------|-----------|
|Student3  |     1     |     3     |     2     |
|----------|-----------|-----------|-----------|

为了使它更加隐秘:行是学生/代理,列代表项目/任务。显然一个项目可以分配给一个学生。简而言之,这就是我的项目。这些字段表示学生对项目投入的偏好权重(范围从1到10)。如果空白,那个学生不想要该项目,并且没有机会分配这个项目。

To make it less cryptic: the rows are the Students/Agents and the columns represent Projects/Task. Obviously ONE project can be assigned to ONE student. That, in short, is what my project is about. The fields represent the preference weights the students have placed upon the projects (ranging from 1 to 10). If blank, that student does not want that project and there's no chance of him/her being assigned such.

无论如何,我的数据存储在字典中。特别是学生和项目词典:

Anyway, my data is stored within dictionaries. Specifically the students and projects dictionaries such that:

students[student_id] = Student(student_id, student_name, alloc_proj, alloc_proj_rank, preferences) 
    where preferences is in the form of a dictionary such that
        preferences[rank] = {project_id}


b $ b

and

projects[project_id] = Project(project_id, project_name)

我知道 sorted(students.keys())会给我将填充行标签和 sorted(projects.keys())的所有学生ID的排序列表将给我列出我需要填充列标签。因此,对于每个学生,我会进入他们的偏好字典,匹配适用的项目的排名。我可以做到这一点。

I'm aware that sorted(students.keys()) will give me a sorted list of all the student IDs which will populate the row labels and sorted(projects.keys()) will give me the list I need to populate the column labels. Thus for each student, I'd go into their preferences dictionary and match the applicable projects to ranks. I can do that much.

我失败的地方是理解如何创建.csv文件。任何帮助,指针或良好的教程将非常感激。

Where I'm failing is understanding how to create a .csv file. Any help, pointers or good tutorials will be highly appreciated.

推荐答案

查看csv模块。基本上,你只需要得到你的数据到某种序列(列表,元组等),然后你可以做 csv.writerow()

Check out the csv module. Basically, you just need to get your data into some kind of sequence (list, tuple, etc.), and then you can just do csv.writerow()

import csv
cot=csv.writer(open('file.csv','wb'))

tmp=[['','Project 1','Project 2','Project 3'],
     ['Student1','','2','1'],
     ['Student2','1','2','3'],
     ['Student3','1','3','2']]
for t in tmp:
    cot.writerow(t)

这篇关于使用Python获取以下示例的CSV输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆