Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python和excel文件_Python_Excel - Fatal编程技术网

Python和excel文件

Python和excel文件,python,excel,Python,Excel,我有一个包含3列的excel文件。 请举例如下 Name Produce Number Adam oranges 6 bob Apples 5 Adam Apples 4 steve Peppers 7 bob Peppers 16 Adam oranges 5 我需要以这种方式在python中生成总和 Name Produce Number A

我有一个包含3列的excel文件。
请举例如下

Name      Produce     Number
Adam      oranges     6
bob       Apples      5
Adam      Apples      4
steve     Peppers     7
bob       Peppers     16
Adam      oranges     5
我需要以这种方式在python中生成总和

Name      Produce     Number
Adam      oranges     11
bob       apples      5
steve     peppers     7
etc
我是python新手,正在试图弄清楚如何编写每个人的输出?有没有一种简单的方法可以从excel中收集这些数据?

1)使用


2) 在记录上迭代,并在dictionary()中使用复合键(Name,product)来累加总和

打开Excel文件后,应该非常简单。 如果保存为.csv文件,请使用以下文档:


使用此链接,然后对记录进行迭代,并获得每个名称和类型的总和:

下面是它的代码:

如果您有任何问题(或者我犯了任何错误),请务必查看评论并发回此处


如果您知道在Python库中的什么位置可以查看,那么这是非常容易的。你已经用Python打开了Excel文件吗?是的,我可以用Excel打开文件,但是我必须先打开Excel文件,才能将其写入Python吗?等等,你可以改写一下:“才能将其写入Python吗?”我需要总数(上面的第二个示例)在python+1中生成,用于
csv
——比
xlrd
更易于使用——特别是如果您不关心格式,通常人们不会将其保留在excel中,因为您可以轻松地将其保存为.csv。如何将数字列相加,我对此有问题。我希望所有包含“bob”“apples”的行都是包含这两行的所有行的总和?对不起,可能真的很容易没问题。。。对不起,我很忙,我现在就做。是的,我看到一个问题。。。一秒钟,我正试图修复它。。。。对不起,我只是发送了我没有真正测试的东西。请再看一次。我修正了密码。另外,请确保所有缩进都与此处键入的缩进完全相同。抱歉,将代码复制到StackOverflow时错过了缩进。。。再次查看我对代码所做的编辑..让我们单击聊天中的“继续讨论”。
import csv

file  = open('names.csv', "rb") #Open CSV File in Read Mode
reader = csv.reader(file)      #Create reader object which iterates over lines

class Object:                   #Object to store unique data
    def __init__(self, name, produce, amount):
        self.name = name
        self.produce = produce
        self.amount = amount

rownum = 0 #Row Number currently iterating over
list = []  #List to store objects

def checkList(name, produce, amount):

    for object in list:  #Iterate through list        
        if object.name == name and object.produce == produce:  #Check if name and produce combination exists
            object.amount += int(amount) #If it does add to amount variable and break out
            return

    newObject = Object(name, produce, int(amount)) #Create a new object with new name, produce, and amount
    list.append(newObject)  #Add to list and break out


for row in reader:  #Iterate through all the rows
    if rownum == 0:  #Store header row seperately to not get confused
        header = row
    else:
        name = row[0]  #Store name
        produce = row[1]  #Store produce
        amount = row[2]  #Store amount

        if len(list) == 0:  #Default case if list = 0
            newObject = Object(name, produce, int(amount))
            list.append(newObject)
        else:  #If not...
            checkList(name, produce, amount)


rownum += 1

for each in list: #Print out result
    print each.name, each.produce, each.amount

file.close() #Close file