使用 Python 获取以下示例的 CSV 输出

发布于 2024-09-03 15:18:25 字数 1427 浏览 2 评论 0原文

我带着我正在进行的学生项目分配问题再次回来。感谢 Moron(与他的名字不符),我对项目的评估部分有了一些指导。

根据分配问题和匈牙利算法的想法,我想以 .csv 文件的形式表达我的数据,该文件最终会在电子表格中看起来像这样。这是基于我在此处看到的结构。

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

为了使其不那么神秘:行是学生/代理,列代表项目/任务。显然,一个项目可以分配给一名学生。简而言之,这就是我的项目的目的。这些字段代表学生对项目的偏好权重(范围从 1 到 10)。如果为空,则该学生不想要该项目,并且他/她不可能被分配这样的项目。

无论如何,我的数据存储在字典中。具体来说,学生和项目字典:

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}

并且

projects[project_id] = Project(project_id, project_name)

我知道 sorted(students.keys()) 将为我提供所有学生 ID 的排序列表,该列表将填充行标签和 sorted(projects.keys()) 将为我提供填充列标签所需的列表。因此,对于每个学生,我都会进入他们的偏好字典并将适用的项目与排名相匹配。我能做到这么多。

我失败的地方是理解如何创建 .csv 文件。任何帮助、指示或好的教程将不胜感激。

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.

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     |
|----------|-----------|-----------|-----------|

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}

and

projects[project_id] = Project(project_id, project_name)

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅沫记忆 2024-09-10 15:18:25

查看 csv 模块。基本上,您只需要将数据放入某种序列(列表、元组等)中,然后您就可以执行 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)

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)
彡翼 2024-09-10 15:18:25

csv 模块就是为此而设计的。

The csv module was made for just that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文