[英]Print Dictionary to CSV file with Python
我想將字典TimeSheet
打印到我的CSV文件中。 但是,它僅將最后一行寫入我的CSV文件。 我怎樣才能解決這個問題? 我可以在控制台中從TimeSheet
打印所有內容,但不是所有的字典都可以打印為CSV。
import glob
import openpyxl
import csv
#loops through .xlsx files in folder path
path = 'C:/ExcelFolder/*.xlsx'
files = glob.glob(path)
for file in files:
#selects specific cells in title sheet.
wb = openpyxl.load_workbook(file)
sheet = wb.get_sheet_by_name('Sheet2')
Week = sheet.cell(row=1, column=1).value
Date = sheet.cell(row=2, column=1).value
Name = sheet.cell(row=4, column=2).value
Title = sheet.cell(row=5, column=2).value
Site = sheet.cell(row=6, column=2).value
LocID = sheet.cell(row=7, column=2).value
for n in range(2, 9):
sheets = wb.worksheets[n]
Days = wb.worksheets[n]
for i in range(1, 57):
From = sheets.cell(row=i, column=1).value
To = sheets.cell(row=i, column=2).value
Activity = sheets.cell(row=i, column=3).value
TimeSheet = {'Week': Week, 'Date': Date, 'Name': Name, 'Title': Title, 'Site': Site, 'LocID': LocID, 'Days': Days, 'From': From, 'To': To, 'Activity': Activity}
with open('TestOutput.csv', 'w') as csvfile:
TimeSheet = {'Week': Week, 'Date': Date, 'Name': Name, 'Title': Title, 'Site': Site, 'LocID': LocID,
'Days': Days, 'From': From, 'To': To, 'Activity': Activity}
fieldnames = ['Week', 'Date', 'Name', 'Title', 'Site', 'LocID', 'Days', 'From', 'To', 'Activity']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerow(
{'Week': Week, 'Date': Date, 'Name': Name, 'Title': Title, 'Site': Site, 'LocID': LocID, 'Days': Days, 'From': From, 'To': To, 'Activity': Activity})
print(TimeSheet)
控制台輸出:
{'Site': 'moon LV-426', 'Activity': None, 'From': datetime.time(18, 45), 'Title': 'Private Hudson', 'Week': 'Week 3', 'To': datetime.time(19, 0), 'Days': <Worksheet "Saturday">, 'Name': 'Bill Paxton', 'Date': '2016/5/22-2016/5/28', 'LocID': '4220A'}
{'Site': 'moon LV-426', 'Activity': None, 'From': datetime.time(19, 0), 'Title': 'Private Hudson', 'Week': 'Week 3', 'To': datetime.time(19, 15), 'Days': <Worksheet "Saturday">, 'Name': 'Bill Paxton', 'Date': '2016/5/22-2016/5/28', 'LocID': '4220A'}
{'Site': 'moon LV-426', 'Activity': None, 'From': datetime.time(19, 15), 'Title': 'Private Hudson', 'Week': 'Week 3', 'To': datetime.time(19, 30), 'Days': <Worksheet "Saturday">, 'Name': 'Bill Paxton', 'Date': '2016/5/22-2016/5/28', 'LocID': '4220A'}
{'Site': 'moon LV-426', 'Activity': None, 'From': datetime.time(19, 30), 'Title': 'Private Hudson', 'Week': 'Week 3', 'To': datetime.time(19, 45), 'Days': <Worksheet "Saturday">, 'Name': 'Bill Paxton', 'Date': '2016/5/22-2016/5/28', 'LocID': '4220A'}
問題可能是您為每次迭代都重新創建了CSV文件。
當您移動包含CSV文件的創建文件時,它應該可以工作。 標題線從內循環出來像這樣:
import glob
import openpyxl
import csv
#loops through .xlsx files in folder path
with open('TestOutput.csv', 'w') as csvfile:
fieldnames = ['Week', 'Date', 'Name', 'Title', 'Site', 'LocID', 'Days', 'From', 'To', 'Activity']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
path = 'C:/ExcelFolder/*.xlsx'
files = glob.glob(path)
for file in files:
#selects specific cells in title sheet.
wb = openpyxl.load_workbook(file)
sheet = wb.get_sheet_by_name('Sheet2')
Week = sheet.cell(row=1, column=1).value
Date = sheet.cell(row=2, column=1).value
Name = sheet.cell(row=4, column=2).value
Title = sheet.cell(row=5, column=2).value
Site = sheet.cell(row=6, column=2).value
LocID = sheet.cell(row=7, column=2).value
for n in range(2, 9):
sheets = wb.worksheets[n]
Days = wb.worksheets[n]
for i in range(1, 57):
From = sheets.cell(row=i, column=1).value
To = sheets.cell(row=i, column=2).value
Activity = sheets.cell(row=i, column=3).value
TimeSheet = {'Week': Week, 'Date': Date, 'Name': Name, 'Title': Title, 'Site': Site, 'LocID': LocID, 'Days': Days, 'From': From, 'To': To, 'Activity': Activity}
writer.writerow(
{'Week': Week, 'Date': Date, 'Name': Name, 'Title': Title, 'Site': Site, 'LocID': LocID, 'Days': Days, 'From': From, 'To': To, 'Activity': Activity})
print(TimeSheet)
問題是,以“ w”模式為每一行打開了“ TestOutput.csv”,這將截斷文件(請參閱https://docs.python.org/3/library/functions.html#open )。 它僅寫入最后一行,因為所有其他行均被刪除。
一目了然,您需要在遍歷文件列表之前將調用移至open()和writeheader()。
如之前的回復所述,請先創建CSV文件。
如果您希望單個csv合並excel文件中的所有數據,則DAXaholic的解決方案應該可以使用。
如果您希望每個excel文件都有一個csv文件,則以下內容可能會有所幫助:
import glob
import openpyxl
import csv
# loops through .xlsx files in folder path
path = 'C:/ExcelFolder/*.xlsx'
files = glob.glob(path)
fieldnames = ['Week', 'Date', 'Name', 'Title', 'Site', 'LocID', 'Days', 'From', 'To', 'Activity']
for file in files:
# selects specific cells in title sheet.
wb = openpyxl.load_workbook(file)
sheet = wb.get_sheet_by_name('Sheet2')
Week = sheet.cell(row=1, column=1).value
Date = sheet.cell(row=2, column=1).value
Name = sheet.cell(row=4, column=2).value
Title = sheet.cell(row=5, column=2).value
Site = sheet.cell(row=6, column=2).value
LocID = sheet.cell(row=7, column=2).value
# append the extension .csv to the current filename
csvfilename = "{}.csv".format(file)
with open(csvfilename, 'w') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for n in range(2, 9):
sheets = wb.worksheets[n]
Days = wb.worksheets[n]
for i in range(1, 57):
From = sheets.cell(row=i, column=1).value
To = sheets.cell(row=i, column=2).value
Activity = sheets.cell(row=i, column=3).value
TimeSheet = {'Week': Week, 'Date': Date, 'Name': Name, 'Title': Title, 'Site': Site, 'LocID': LocID, 'Days': Days, 'From': From, 'To': To, 'Activity': Activity}
writer.writerow(TimeSheet)
print(TimeSheet)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.