gpt4 book ai didi

python - xls Python 的元组列表

转载 作者:行者123 更新时间:2023-12-04 20:06:56 24 4
gpt4 key购买 nike

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

我的 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()

最佳答案

直接来自docs对于csv模块

import csv

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

for x in sorted_list:
fwriter.writerow(x)

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

关于python - xls Python 的元组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18210341/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com