在 python 中使用 Excel 表格格式

发布于 2024-11-16 11:31:01 字数 528 浏览 1 评论 0原文

我有一个保存为 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 技术交流群。

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

发布评论

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

评论(5

瞎闹 2024-11-23 11:31:01

是的,我们可以帮助您编写这样的程序。

我们不会为您编写程序(这不是 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!

稍尽春風 2024-11-23 11:31:01

最好的选择是使用 python 内置的 csv 包。它会让您遍历行,每次都会给您一个列表,如下所示:

import csv
candy_reader = csv.reader(open('candy.csv', 'rb'))

for row in candy_reader:
  if row[0] in candy_to_find:
    monday_total += row[1]
    # etc.    

我没有初始化几个变量,但我认为这应该给您基本的想法。

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:

import csv
candy_reader = csv.reader(open('candy.csv', 'rb'))

for row in candy_reader:
  if row[0] in candy_to_find:
    monday_total += row[1]
    # etc.    

I didn't initialize several variables, but I think this should give you the basic idea.

み零 2024-11-23 11:31:01

也许使用 csv 模块

我想分隔符是制表符(\t )

import csv
reader = csv.reader(open('csv_file.csv', 'rb'), delimiter='\t')
for row in reader:
    print row

>>>['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']

Maybe with the csv module

I suppose the delimiter is the tab character (\t)

import csv
reader = csv.reader(open('csv_file.csv', 'rb'), delimiter='\t')
for row in reader:
    print row

>>>['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']
少女情怀诗 2024-11-23 11:31:01

除了其他答案之外,您可能还想看看 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.

高冷爸爸 2024-11-23 11:31:01

一些有用的位:

如果您有一个类似列表的列表

[['marsbar', 0, 1, 2, 3, 4], ['twix', 3, 4, 5, 6, 7]]

(您应该能够通过使用 csv 模块获得),

您可能希望将其转换为字典,其中每个列表的第一项list用于key,其余组成value。您可以使用类似的方法来完成此操作

dict((x[0], x[1]) for x in list_of_lists)

。您也可以使用列表理解来查找多个键:

[the_dict[key] for key in key_list]

这将为您提供一个列表列表,您可以在其中对每个列表的第一个元素、第二个元素等求和。为此,我们“压缩”列表以创建包含所有第一个元素、所有第二个元素等的列表列表,然后对内部列表求和。

[sum(x) for x in zip(*the_requested_candybars)]

zip() 函数接受多个参数;这里的 * 将列表列表转换为多个列表参数。

我们可以再次zip以将周名称与总和相匹配。

Some useful bits:

If you have a list of lists like

[['marsbar', 0, 1, 2, 3, 4], ['twix', 3, 4, 5, 6, 7]]

(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

dict((x[0], x[1]) for x in list_of_lists)

You can look up multiple keys with a list comprehension as well:

[the_dict[key] for key in key_list]

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.

[sum(x) for x in zip(*the_requested_candybars)]

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.

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