删除文件中包含多行的行 [英] Delete lines found in file with many lines

查看:149
本文介绍了删除文件中包含多行的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多行(1008)的Excel文件(.xls),我正在寻找与2010年有关的行。

I have an Excel file (.xls) with many lines (1008), and I'm looking for lines that have anything with 2010.

例如,有一行包含01/06/2010,因此该行将被删除,单元格将留空。

For example, there is a line that contains 01/06/2010, so this line would be deleted, leaving the cell blank.

对于此示例,所有这些都将被删除。我尝试至少阅读该文件,但我得到了一个丑陋的错误:

For this example, all of these would be deleted. I tried at least reading the file, but I got an ugly error:

def Pesquisar():
    nomeArquivo = open('D:/file.xls', 'r')
    for palavraArquivo in nomeArquivo.readlines():
        print palavraArquivo

结果:

ÐÏࡱ


推荐答案

您无法直接读取Excel文件,因为它不是标准文本文件。您需要使用第三方库,例如 xlrd 。另一种选择是将xls文件导出为csv文件或制表符分隔格式,然后使用python将它们解析为文本文件。

You can't directly read an excel file since it's not a standard text file. You need to use a third party library such as xlrd. Another option would be to export the xls file as a csv file or tab delimited format and then parse them as a text file with python.

这篇关于删除文件中包含多行的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆