在 python 中使用 Excel 表格格式
我有一个保存为 CSV 的 Excel 表格,看起来像(当它在 Excel 中时)这样更长:(
Monday Tuesday Wednesday Thursday Friday
marsbar 9 8 0 6 5
reeses 0 0 0 9 0
twix 2 3 0 5 6
snickers 9 8 0 6 5
格式并不完美,但你明白了要点——糖果棒/天)。我想为 python 编写一个程序,在其中我可以在程序中输入糖果棒名称列表
>>> ['twix', 'reeses']
,它会告诉我在一周中的哪几天吃了这些糖果棒,以及列表中吃了多少糖果棒。当天。结果看起来像这样:
Monday 2
Tuesday 3
Wednesday 0
Thursday 14
Friday 6
有人能帮我写一个这样的程序吗?
I have an Excel table saved as a CSV that looks something like (when it's in Excel) this just longer:
Monday Tuesday Wednesday Thursday Friday
marsbar 9 8 0 6 5
reeses 0 0 0 9 0
twix 2 3 0 5 6
snickers 9 8 0 6 5
(The format isnt perfect, but you get the gist--candy bars/day). I want to write a program for python in which I could input a list of candy bar names in the program
>>> ['twix', 'reeses']
and it would give me the days of the week on which those candy bars were eaten and how many candy bars in the list were eaten on that day. The result would look something like this:
Monday 2
Tuesday 3
Wednesday 0
Thursday 14
Friday 6
could someone help me write such a program?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的,我们可以帮助您编写这样的程序。
我们不会为您编写程序(这不是 StackOverflow 的目的)。
Python 附带了一个 CSV 模块,它将在 Excel 中读取 -保存的 CSV 文件。
您可以将数据存储在字典中,将使检索更容易。
祝你好运!
We can help you write such a program, yes.
We will not write the program for you (that's not what StackOverflow is about).
Python comes with a CSV module, which will read in an Excel-saved CSV file.
You can store your data in a dictionary, which will make it easier to retrieve.
Good luck!
最好的选择是使用 python 内置的
csv
包。它会让您遍历行,每次都会给您一个列表,如下所示:我没有初始化几个变量,但我认为这应该给您基本的想法。
Your best bet is to use the
csv
package built into python. It will let you loop over the rows, giving you a list each time, like so:I didn't initialize several variables, but I think this should give you the basic idea.
也许使用 csv 模块
我想分隔符是制表符(
\t )
Maybe with the csv module
I suppose the delimiter is the tab character (
\t
)除了其他答案之外,您可能还想看看 xlrd 模块,因为如果您不想处理 CSV 业务,它可以直接处理 Excel 电子表格。该模块允许您一次读取整个(或范围)行/列以及抓取单个单元格,您可以迭代所有内容以搜索您想要的内容,然后返回同一行中您想要的任何相关“值” /column 从哪里找到您的搜索条件。
In addition to the other answers, you may also want to take a look at the xlrd module, as it can deal with the excel spreadsheets directly if you'd rather not deal with CSV business. The module allows you to read entire (or ranges) of rows/columns at a time as well as grabbing individual cells and you can iterate through everything to search for what you want and then return whatever related 'values' you want in the same row/column from where you found your search criteria.
一些有用的位:
如果您有一个类似列表的列表
(您应该能够通过使用
csv
模块获得),您可能希望将其转换为字典,其中每个列表的第一项list用于key,其余组成value。您可以使用类似的方法来完成此操作
。您也可以使用列表理解来查找多个键:
这将为您提供一个列表列表,您可以在其中对每个列表的第一个元素、第二个元素等求和。为此,我们“压缩”列表以创建包含所有第一个元素、所有第二个元素等的列表列表,然后对内部列表求和。
zip()
函数接受多个参数;这里的*
将列表列表转换为多个列表参数。我们可以再次
zip
以将周名称与总和相匹配。Some useful bits:
If you have a list of lists like
(which you should be able to get by using the
csv
module)You will probably want to convert it to a dictionary, where the first item of each list is used for the key, and the rest make up the value. You can do this with something like
You can look up multiple keys with a list comprehension as well:
That gives you a list of lists, where you want to sum the first elements of each list, the second elements, etc. To do that, we 'zip' the lists to make a list of lists with all the first elements, all the second elements etc., and then sum the inner lists.
The
zip()
function takes multiple arguments; the*
here turns the list of lists into several list arguments.We can
zip
again to match up the week names with the sums.