2017-07-07 63 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()