Python从for循环打印到excel或csv
我一直致力于此代码:Python从for循环打印到excel或csv,python,Python,我一直致力于此代码: Section = {'East':['Alan','Bob'], 'North':['Alan','Michael'], 'South':['Tom'], 'West':['Bob','Michael','Tom']} Name = {'Alan':['Subaru','Chevvy','Honda'], 'Bob':['Toyota','Honda','Camry'], '
Section = {'East':['Alan','Bob'],
'North':['Alan','Michael'],
'South':['Tom'],
'West':['Bob','Michael','Tom']}
Name = {'Alan':['Subaru','Chevvy','Honda'],
'Bob':['Toyota','Honda','Camry'],
'Michael':['Camry','Ford'],
'Tom':['Ford','Toyota']}
Inventory = {'East':['Toyota','Honda','Camry'],
'North':['Ford','Chevvy','Ferrari','Subaru'],
'South':['Subaru','Acura','Lexus','BMW'],
'West':['Ford','Subaru','Camry']}
for name,sections in Section.items():
for section in sections:
haveInventory = Name[name]
needInventory = Inventory[section]
for inventory in needInventory:
if inventory not in haveInventory:
print(str(name) + ' ' + str(section) + ' ' + str(inventory))
我改变了这两行:
for name,sections in Section.items():
for section in sections:
To
for sections,names in Section.items():
for name in names:
看看这个:
for name,sections in Section.items(): # ('East', ['Alan','Bob'])
for section in sections:
haveInventory = Name[name] # Name['East'] Causes error
needInventory = Inventory[section]
正如您所看到的,在那个特定的迭代中,您试图从字典
'haveInventory'
中获取键'East'
的值,但该字典没有该键。它没有运行,因为这对项将是section、name,而不是name、sections,例如'East'、['Alan'、'Bob']
。。。因此,当您在其他词典中查找时,您正在查找名称['East']
(关键错误),库存['Alan']
(关键错误)…代码不运行这意味着什么?如何将结果导出到excel或csv文件?问题到底是什么?你有没有研究过如何用Python编写CSV文件?请看,谢谢。这正是我要找的。不知道如何用panda dataframe解析结果以打印出来。我很高兴听到这个消息。注意:如果数据集太大,以这种方式保存可能会导致内存错误。最好逐行保存。如何逐行保存数据?-nvm,我看到下面的代码和逐行指令。谢谢:)你可以从Pygirl那里查看下面的答案。另外,请查看此帖子stackoverflow.com/questions/8078330/csv-writing-in-loop。如果需要,您还可以在保存csv文件时添加时间戳。这几乎与Pygirl相同,唯一的区别是csv名称中有时间戳(以防您需要它或希望防止覆盖现有文件)。
Section = {'East': ['Alan', 'Bob'],
'North': ['Alan', 'Michael'],
'South': ['Tom'],
'West': ['Bob', 'Michael', 'Tom']}
Name = {'Alan': ['Subaru', 'Chevvy', 'Honda'],
'Bob': ['Toyota', 'Honda', 'Camry'],
'Michael': ['Camry', 'Ford'],
'Tom': ['Ford', 'Toyota']}
Inventory = {'East': ['Toyota', 'Honda', 'Camry'],
'North': ['Ford', 'Chevvy', 'Ferrari', 'Subaru'],
'South': ['Subaru', 'Acura', 'Lexus', 'BMW'],
'West': ['Ford', 'Subaru', 'Camry']}
import pandas as pd
result=pd.DataFrame()
NAME = []
SECTION = []
INVENTORY = []
for section, names in Section.items():
for name in names:
haveInventory = Name[name]
needInventory = Inventory[section]
for inventory in needInventory:
if inventory not in haveInventory:
print(str(name) + ' ' + str(section) + ' ' + str(inventory))
NAME.append(name)
SECTION.append(section)
INVENTORY.append(inventory)
data = {'Name': NAME, 'Section': SECTION, 'inventory': INVENTORY}
result = pd.DataFrame(data, columns=['Name', 'Section', 'inventory'])
result.to_csv('result.csv')
for name,sections in Section.items(): # ('East', ['Alan','Bob'])
for section in sections:
haveInventory = Name[name] # Name['East'] Causes error
needInventory = Inventory[section]
Section = {'East': ['Alan', 'Bob'],
'North': ['Alan', 'Michael'],
'South': ['Tom'],
'West': ['Bob', 'Michael', 'Tom']}
Name = {'Alan': ['Subaru', 'Chevvy', 'Honda'],
'Bob': ['Toyota', 'Honda', 'Camry'],
'Michael': ['Camry', 'Ford'],
'Tom': ['Ford', 'Toyota']}
Inventory = {'East': ['Toyota', 'Honda', 'Camry'],
'North': ['Ford', 'Chevvy', 'Ferrari', 'Subaru'],
'South': ['Subaru', 'Acura', 'Lexus', 'BMW'],
'West': ['Ford', 'Subaru', 'Camry']}
import pandas as pd
result=pd.DataFrame()
NAME = []
SECTION = []
INVENTORY = []
for section, names in Section.items():
for name in names:
haveInventory = Name[name]
needInventory = Inventory[section]
for inventory in needInventory:
if inventory not in haveInventory:
print(str(name) + ' ' + str(section) + ' ' + str(inventory))
NAME.append(name)
SECTION.append(section)
INVENTORY.append(inventory)
data = {'Name': NAME, 'Section': SECTION, 'inventory': INVENTORY}
result = pd.DataFrame(data, columns=['Name', 'Section', 'inventory'])
result.to_csv('result.csv')
from time import gmtime, strftime, time
import csv
current_time = strftime("%Y-%m-%d_%H-%M-%S", gmtime())
with open('result_'+ current_time + '.csv','w') as f1:
writer = csv.writer(f1, delimiter='\t',lineterminator='\n',)
for section, names in Section.items():
for name in names:
haveInventory = Name[name]
needInventory = Inventory[section]
for inventory in needInventory:
if inventory not in haveInventory:
print(str(name) + ' ' + str(section) + ' ' + str(inventory))
writer.writerow([str(name) + ' ' + str(section) + ' ' + str(inventory)])