2016-06-15 211 views
1

我想將我的字典TimeSheet打印到我的CSV文件中。但是,它只將最後一行寫入我的CSV文件。我怎樣才能解決這個問題?我可以在控制檯中打印我的TimeSheet中的所有內容,但並非所有字典都打印爲CSV。用Python打印字典到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輸出: enter image description here

回答

2

這個問題可能是您重新每次迭代的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) 
+0

我明白你的意思,但是當我提出公開徵集到內環未打印或寫入到CSV文件。 – wisenhiemer

+0

我不是在說移動到內部循環,而是在所有的循環中出現 – DAXaholic

+0

啊我現在看到了,謝謝。 – wisenhiemer

1

的問題是「TestOutput.csv」打開與「W」模式,這將截斷該文件(見https://docs.python.org/3/library/functions.html#open)每一行。它只寫最後一行,因爲所有其他行都被刪除了。

在迭代文件列表之前,您需要將調用移至open()和writeheader()。

1

如前面的回覆中所述,請事先創建CSV文件。

如果您希望單個csv整合來自excel文件的所有數據,那麼DAXaholic的解決方案應該可以工作。

如果你想有一個CSV文件爲每個excel文件,以下可能會有幫助:

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)