Delete lines found in file with many lines

2019-08-25 05:42发布

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

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

enter image description here

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

Result:

ÐÏࡱ

2条回答
ら.Afraid
2楼-- · 2019-08-25 05:57

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.

查看更多
走好不送
3楼-- · 2019-08-25 05:59

Excel files use a special format that doesn't lend itself to plain text parsing.

This discussion might point you in the right direction, as far as libraries for handling .xls formats go: Reading/parsing Excel (xls) files with Python

查看更多
登录 后发表回答