python怎么删除exce的l某些行?例如这些行的某一格中带有字符串x或y或z...

按 指定行数 分页读取好处理:

for line in X:

python删除行_python删除行列python删除行_python删除行列


python删除行_python删除行列


python删除行_python删除行列


楼上的是理解错了吧,我的理解觉得是楼主认为在一行内出现匹配的字符串,而不是整行和字符串匹配。把if 那一行修改一下 if len(line.strip().split("pattern string")) == 1:

if "yyy" in line:#如果字符yyy在某一行中就做什么事情。。。。。

do soming

作excel的代码网上很多,我找了一个你参考下

from win32com.client import constants, Dispatch

class EasyExcel:

def __init__(self, filename=None):

self.xlApp = Dispatch('Excel.Application')

if filename:

self.filename = filename

self.xlBook = self.xlApp.Workbooks.Open(filename)

else:

print "please input the filename"

def close(self):

self.xlBook.Close(SeChanges=0)

del self.xlApp

def getCell(self, sheet, row, col):

"Get value of one cell"

sht = self.xlBook.Worksheets(sheet)

return sht.Cells(row, col).Value

def getRange(self, sheet, row1, col1, row2, col2):

"return a 2d array (i.e. tuple of tuples)"

sht = self.xlApp.Worksheets(sheet)

[代码] [Python]代码

from easyExcel import EasyExcel

content=excelProxy.getRange("sheet1",1,1,2,2)

print content

python遍历文本并删除特定内容

def iterpage(istream, pagesize):

buffer = [a.add(line.strip())]

for data in istream:

buffer.append(data)

if len(buffer)>=pagesize:

yield buffedf.drop(index, axis=0, inplace=True)r

buffer = []

if buffer:

yield buffer

with open("source.txt", 'rt') as handle:

for page in iterpage(handle, 1000):

print page # or your business logical

print "-"32 # page break删除文本文件的前N行:

buffer = []

with open(filename, 'rt') as handle:

if ln < headlines:

continue

buffer.append(ln)

handle.wriines(buffer)或者:

def getandremovehead(filename, headlines):

with open(filename, 'rt') as handle:

buffer = handle.readlines()

handle.wriines(buffer[headlines:])

return buffer[:headlines]但遇到大文本文件时,删除其中N行不是很理想的业务方案

python3 pandas drop函数一直报错搜索不到我要删除的行

Aug 24 2018 10:30:00 IFNET 1 IF_LINKDOWN:OID 1.3.6.1.6.3.1.1.5.3 Intece 7 turned into DOWN state.

根据报错来看, 应该是df=df.dfor m in a:rop(df.columns[0])这句有问题的吧. 这句是删除行, 但因为行中没有df.columns[0]这个index, 所以会报错.

python 读取CSV数据,删除有特殊值的行

是不是想要直接打开文件夹,那使用如下命令就可以

with open('1.csv') as f:

lists[i] = lists[i].strip('n')

r = csv.reader(f)

for i in r:

if '999999' in i:

pass

else:

print(i)

以上python3代码,1.csv为你的文件名。居然不能缩进?自己缩进吧,应该看的出来吧

pandas如何删除指定行

excelProxy = EasyExcel("d:/test.xls")

1、创建pandas模块实例文件,导入相应的模块包,然后分别调用对应的方法生成矩阵的行和列。

3、如果想要获取矩阵的后面的几行记录,可以使用tail()方法,添加参数代码多少行。

4、再次保存代码并运行,由于tail()方法传入了参数2,代表获取矩阵两行。

5、如果想要获取矩阵列的索引值,可以使用模块中的index属性。

6、再次保存运行文件,在控制台可以查看到打印的结果为日期序列值。

删除某行的方法是df.drop('索引值',axis=0)

通过程序来判断,并得到索引值

判断st股票:'ST' in row['name'] #ST要大写,row是迭代dataframe时候的变量

判断3开头的股票: '3' in row['code'][0]

#大概的程序就是下面这样,df是你得到的dataframe。code是股票代码

for indoutfp.close()ex,row in df.iterrows():

if 'ST' in row['name']:

elif '3' in row['code'][0]:

python删除某一行字符前面的内容

Aug 24 2018 10:36:31 INFO 4 IC_LOGFILE_AGING:OID 1.3.6.1.4.1.2011.5.25.212.2.2 One log file is deleted due to aging.

import re

设置参数 axis=1 或者 axis=columns 删除缺失列。

a='''Aug 24 2018 10:29:59 SRM_ENTITY 4 CARDINVALIDRESUI4E:OID 1.3.6.1.4.1.2011.5.25.219.2.3.6 The card recovered from total failure.

Aug 24 2018 10:30:00 IFNET 4 IF_CARD_CFI4OVER:OID 1.3.6.1.4.1.2011.5.25.41.3.27 The configuration of the intece card is restored.

Aug 24 2018 10:30:47 LINE 5 VTYUSERLOGOUT:OID 1.3.6.1.4.1.2011.5.25.207.2.4 A user logout.

Aug 24 2018 10:38:52 CGN 4 CGN2USER_PORT_NUI4_TRAP:Sloc=7,Vcpu=0:OID 1.3.6.1.4.1.2011.5.25.240.2.4 The number of pores assigned to Cl

Aug 24 2018 10:56:52 LINE 5 VTYUSERLOGIn7:OID 1.3.6.1.4.1.2011.5.25.207.2.2 A user login.

Aug 24 2018 10:56:58 LINE 5 VTYUSERLOGOUT:OID 1.3.6.1.4.1.2011.5.25.207.2.4 A user logout.

Aug 24 2018 11:07:30 SNMP 4 AUTHFAIL:OID 1.3.6.1.6.3.1.1.5.5 authenticationFailure.'''

arr=a.split('

[' '.join(x.split(' ')[:3]+["OID"]+x.split(' ')[7:8]) for x in arr]

python怎么删除包含指定中文的行

fa=open('del.txt','rt')

fb=open('output.txt','rt')

fc=open('c.txt','wt')

a=set()

for line in fa:

for line in fb:

exist=False

if line.startswith(m):

exist不知道具体的csv的结构, 所以只能帮到这种程度了. 然后就是可以直接在pd.read_csv里面写文件路径, 上面的open语句有点多余了.=True

break

if existif li.split():: print('skip '+line)

else: fc.write(line)

fb.close()

fc.close()

这段代码是借用其他网友的,看了下没有错误,省的自己敲了,你看哪里不明白。或者你把自己的代码粘上来帮你检查也行。

两个for循环跑吧。。。

python 如何去除文本中多余的换行符

遍历excel的一个数据区域X。

Python读取一个文本文件,删除文本文件的空行代码如下:

def delblankline(infile, outfile):

""" Delete blanklines of infile """

infp = open(infile, "r")

outfp = open(outfile, "w")

lines = infp.readlines()

for li in lines:

outfp.wriines(li)

infp.close()

#调用示例

if __name__ == "__main__":

delblankline("1.txt","2.txt")

f = open(路径,'r')

lists = f.readlines()

for i in range(len(lists)):

f.close()

若文件内容在大字符串content中:

[ln for ln in content.splitline文件夹路径')s() if ln.strip()]

若保存前处理的各行在列表 processedlist中:

[l for l in processlist if l.strip()]

python--pandas删除

import csv

drop 方法是pandas中删除行或列的方法。

根据 索引名 删除目标行。

当需要根据索引位置删除时,可以使用 index 属性来组合完成。

根据 列名 删除目标列,同时需要设置 axis=1 或者 columns 。

当需要根据列位置删除时,可以使用 columns 属性来组合完成。

删除列也可以用关键字 del 实现,每次只能删除一列,且删除列后,原数据发生改变。

同时删除行和列,需要为行使用 index 参数,为列使用 columns 参数。

当数据框有多重索引时,删除行时,需要设置 ll 参数。

dropna 为删除缺失值的方法。

默认会删除包含缺失值的所有行。

可设置 how , thresh , subset 参数控制删除的行为。

同样,可以设置 how , thresh , subset 参数来控制删除缺失列的行为。

删除fa.close()重复值用 drop_duplicates 方法实现。

设置 subset 参数,根据列删除重复行。

设置 ignore_index=True 可以对删除重复行后的数据索引重排序。

python中删除数据框某个行时,语法df.drop('列名',axis=1)中,使用axis=1,axis=1不是表示行么?

Aug 24 2018 10:30:47 HWCM 4 CFGCHANGE:OID 1.3.6.1.4.1.2011.6.10.2.1 Configure changed.

代表列,不是行,不是行,不是行

data.drofor i, ln in enumerate(handle):p(n)可以删除第i行

import pandas as pd

data=pd.DataFrame([[1,2,3],[4,5,6]])

print data.drop(0)输出结果为 0 1 21 4 5 6