2017-07-07 65 views
0

這裏刪除行是我的函數:無法從.csv文件pyexcel

 def prepare_file(time, mkt): 
     # renames file to corresponding market name 
     global previous_time 
     for file in glob.glob(os.getcwd()+'\Reports\*'): 
      # if it's the most recently downloaded file 
      if time > previous_time: 
       previous_time = time 
       # remove rows for properties that have not changed status 
       sheet = pyexcel.get_sheet(file_name=file) 
       for row in sheet: 
        if row[1] in changed_addresses: 
         pass 
        else: 
         del row 
       # save file as correct name 
       sheet.save_as(
        os.getcwd() + '\\Reports\\' + mkt[0] + '.csv' 
       ) 
       os.remove(file) 

的想法是找到一個目錄中的最近下載的文件,打開它,刪除不包含地址的所有行從changed_addresses列表中,並將其保存爲包含在mkt列表中的字符串。

一切工作正常,除了行的刪除。它正確地遍歷它們,並理解何時應刪除一行,但輸出的文件仍包含應該消失的所有行。

del row對於這種情況不正確的命令?

+0

您的文件是csv文件還是Excel表格? –

+0

這是一個csv文件。 –

+0

'del row'只是刪除了python中的變量行,它不會刪除文件中的行。該行保持不變。您可以將行寫入新的CSV文件,並用臨時名稱保存,然後將其重命名爲原始文件。你特別想用'pyexcel'還是'csv'呢? –

回答

0

使用csv我認爲這應該工作:

import csv 
import os 
import glob 

def prepare_file(time, mkt): 
    # renames file to corresponding market name 
    global previous_time 
    for file in glob.glob(os.getcwd()+'\Reports\*'): 
     # if it's the most recently downloaded file 
     if time > previous_time: 
      previous_time = time 
      # remove rows for properties that have not changed status 
      fin = open(file, 'r') 
      fout = open((os.getcwd() + '\\Reports\\' + mkt[0] + '.csv'), 'w', newline='') 
      reader = csv.reader(fin) 
      writer = csv.writer(fout) 

      for row in reader: 
       if row[1] not in changed_addresses: 
        writer.writerow(row) 

      # close files 
      fin.close() 
      fout.close() 

      # remove original 
      os.remove(file) 

所以先用名file打開你的數據文件,後來又用新的名稱保存。

+0

與之前相同的問題...該文件仍包含應該刪除的所有行。我測試了它,看它是否在列表和csv之間找到匹配,並且它確實匹配。只是刪除了問題所在的行。 –

+0

@ZakSingh changed_adresses是什麼樣子。我認爲選擇只是失敗。查看我在if語句上的編輯。如果選擇工作不正常,現在應該以空文件結束。 –

+0

它沒有被清空。這是一個例子changed_addresses看起來像:[「1234街道」,「2345街道」] –

0

pyexcel,您需要使用這個語法:

del sheet.row[index] or del sheet.row[index1, index2, index3] 

下面是示例代碼:

def prepare_file(time, mkt): 
    # renames file to corresponding market name 
    global previous_time 
    for file in glob.glob(os.getcwd()+'\Reports\*'): 
     # if it's the most recently downloaded file 
     if time > previous_time: 
      previous_time = time 
      # remove rows for properties that have not changed status 
      sheet = pyexcel.get_sheet(file_name=file) 
      indices_to_be_removed = [] # <- 
      for index, row in enumerate(sheet): 
       if row[1] in changed_addresses: 
        pass 
       else: 
        indices_to_be_removed # <- 
      # save file as correct name 
      del sheet.row[indices_to_be_removed] # <- 
      sheet.save_as(
       os.getcwd() + '\\Reports\\' + mkt[0] + '.csv' 
      ) 
      os.remove(file) 

另外,的另一種方法是,你可以寫一個過濾器和優勢它可以處理巨大的數據文件與自定義內存佔用:

def filter(file_name, changed_addresses): 
    for row in pyexcel.iget_array(file_name=file_name): 
     if row[1] in changed_addresses: 
      yield row 


def prepare_file(time, mkt): 
    # renames file to corresponding market name 
    global previous_time 
    for file in glob.glob(os.getcwd()+'\Reports\*'): 
     # if it's the most recently downloaded file 
     if time > previous_time: 
      previous_time = time 
      # remove rows for properties that have not changed status 
      pyexcel.isave_as(array=filter(file, changed_addresses), 
          dest_file_name=os.getcwd() + '\\Reports\\' + mkt[0] + '.csv') 
      os.remove(file) 

但請記得在代碼結束時調用。它將關閉所有的csv文件句柄。

pyexcel.free_resources()