2

对不起,如果我做错了什么,我是新来的。

我的 Python 代码有问题。我有一个字典中的 sorted_List 。排序列表看起来像

sorted_Dict = [('158124', 26708), ('146127', 12738), ('21068', 9949), 
('274186', 8255), ('189509', 6550), ('165758', 5346), ...]

我现在想将它们打印在一个 xls 文件中,它应该看起来像

    x             y
'158124'       26708

我必须在 Excell 中绘制它,但我也想在 python 中绘制它(这不是必需的,但很酷),但我不知道如何做到这一点。这是我的整个代码。感谢您的任何帮助

干杯

斯文

# -*- coding: iso-8859-1 -*-
from __future__ import division

import csv
import operator

def computeSoldProducts():
    catalog = csv.reader(open("data/catalog.csv", "r"))
    sales = csv.reader(open("data/sales_3yr.csv", "r"))
    output = open("output.csv", "a")
    catalogIDs = set()
    lineNumber = 0
    # lese katalog
    for line in catalog:
        id = line[0]
        if lineNumber <> 0:
            catalogIDs.add(eval(id))
        lineNumber = 1

    soldItems = set()
    lineNumber = 0
    # lese sales
    for line in sales:
        id = line[6]
        if lineNumber <> 0:
            soldItems.add(eval(id))
        lineNumber = 1

    print "anzahl Produkte:", len(catalogIDs)
    print "verkaufte Produkte", len(soldItems)

    notSoldIDs = catalogIDs - soldItems
    print len(notSoldIDs)

    catalog = csv.reader(open("data/catalog.csv", "r"))
    sales = csv.reader(open("data/sales_3yr.csv", "r"))
    soldDict = {}

    for k in catalog:
        soldDict[str(k[0])] = 0

    for item in sales:
        if str(item[6]) in soldDict:
            soldDict[str(item[6])] +=1


    sorted_soldDict = sorted(soldDict.iteritems(), key=operator.itemgetter(1), reverse=True)

    print sorted_soldDict
    print sorted_soldDict

    for k in sorted_soldDict:
        output.write(sorted_soldDict[k])


    print "done"    
computeSoldProducts()
4

2 回答 2

2

直接来自模块的文档csv

import csv

with open('text.csv', 'wb') as csvfile:
    fwriter = csv.writer(csvfile)

    for x in sorted_list:
        fwriter.writerow(x)

然后,您可以在 excel 中打开此 csv 文件。

于 2013-08-13T13:33:36.683 回答
1

一种替代方法是使用我的库pyexcel,文档在这里: http: //pythonhosted.org//pyexcel/

import pyexcel

sorted_list_of_sets = ....
writer = pyexcel.Writer("output.csv")
writer.write_array(sorted_list_of_sets)
writer.close()

如果使用 pyexcel,您的原始解决方案将变为以下内容:

import pyexcel
import operator


def computeSoldProducts():
    catalog = pyexcel.SeriesReader("data/catalog.csv")
    sales = pyexcel.SeriesReader("data/sales_3yr.csv")

    print "anzahl Produkte:", catalog.number_of_rows()
    print "verkaufte Produkte", sales.number_of_rows()

    product_list = catalog.column_at(0)
    solditem_list = sales.column_at(6)

    soldOnes = []
    for item in solditem_list:
        if item not in soldOnes:
            soldOnes.append(item)
    notSoldIDs = catalog.number_of_rows() - len(soldOnes)
    print notSoldIDs

    print product_list
    print solditem_list

    # initialize the soldDict
    zeros_array = [0] * len(product_list)
    soldDict = dict(zip(product_list, zeros_array))
    for item in solditem_list:
        if item in product_list:
            soldDict[item] += 1

    sorted_soldDict = sorted(soldDict.iteritems(), key=operator.itemgetter(1), reverse=True)

    print sorted_soldDict

    writer = pyexcel.Writer("output.csv")
    writer.write_row(["product", "number"])
    writer.write_array(sorted_soldDict)
    writer.close()
    print "done"

computeSoldProducts()
于 2014-09-18T10:14:49.127 回答