这里删除行是我的函数:无法从.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
对于这种情况不正确的命令?
您的文件是csv文件还是Excel表格? –
这是一个csv文件。 –
'del row'只是删除了python中的变量行,它不会删除文件中的行。该行保持不变。您可以将行写入新的CSV文件,并用临时名称保存,然后将其重命名为原始文件。你特别想用'pyexcel'还是'csv'呢? –