當前位置:首頁 » 編程語言 » python刪除行

python刪除行

發布時間: 2022-01-12 04:35:37

A. python 刪除特定幾行

你之前編寫的Python直接用記事本打開,然後刪除保存就好了呀~

B. 在python中如何刪除文件的某一行


data=open(filename,'rt').readlines()
withopen(filename,'wt')ashandle:
handle.writelines(data[:tobedeleted])
handle.writelines(data[tobedeleted+1:])

C. python 怎麼刪除指定的行和空行

if l[:-1].strip() l是從舊文件里讀出來的每一行,判斷如果不是空行,則把這一行存到列表中,再將新的列表按行寫入新文件。 首先strip()是去除空白字元的意思。 l[:-1].strip()是把這一行中除了最後那個換行符去掉,然後再去掉空白字元得到的字元串 如果去掉換行符和空白符後得到的是空字元串的話,這一行就被拋棄,否則加入新的列表,等待寫入。

D. python怎麼刪除exce的l某些行例如這些行的某一格中帶有字元串x或y或z...

自己幫你寫的,在2.6版本下測試通過,你如果沒安裝pywin32模塊要先下載安裝一下。
希望對你有用!
=======================================================
#本程序在python2.6版本下測試通過

import win32com.client #這里用到win32com.client,需要安裝pywin32模塊
#下載地址 http://sourceforge.net/projects/pywin32/files/pywin32/Build216/

xlApp = win32com.client.Dispatch('Excel.Application') #打開EXCEL,這里不需改動
xlBook = xlApp.Workbooks.Open('D:\\1.xls') #將D:\\1.xls改為要處理的excel文件路徑
xlSht = xlBook.Worksheets('sheet1') #要處理的excel頁,默認第一頁是『sheet1』

n=20 #n改為要處理的表格內容的行數
m=5 #m改為要處理的表格的內容的列數

try: #為了加強程序健壯性,使用try...except方式避免出錯後程序中斷。可以將try、except刪去。
i=1
while i<=n:
for j in range(1,m+1):
temp = str(xlSht.Cells(i,j).Value) #區每行每列各元素
if temp.find('x')>-1 or temp.find('y')>-1 or temp.find('z')>-1: #若其中含有x、y、z,這里可改為其他判斷條件
xlSht.Rows(i).Delete() #將滿足條件的元素所在行 整行刪除
i=i-1
n=n-1 #由於刪除了一行,同時修改相關參數,避免判斷遺漏
break
i=i+1 #行標加1,繼續判斷下一行
except:
print 'runerror'

xlBook.Close(SaveChanges=1) #完成 關閉保存文件
del xlApp
=======================================================

E. python怎麼刪除包含指定中文的行

fa=open('del.txt','rt')
fb=open('output.txt','rt')
fc=open('c.txt','wt')
a=set()
for line in fa:
a.add(line.strip())
fa.close()
for line in fb:
exist=False
for m in a:
if line.startswith(m):
exist=True
break
if exist: print('skip '+line)
else: fc.write(line)
fb.close()
fc.close()
這段代碼是借用其他網友的,看了下沒有錯誤,省的自己敲了,你看哪裡不明白。或者你把自己的代碼粘上來幫你檢查也行。

F. Python中刪除文檔中的固定行

import re

list = []
matchPattern = re.compile(r'.+:\sdana') #簡陋的reg用來匹配包含'dana'的那行
file = open('source.txt','r') #假設'source.txt'是你要處理的文檔
while 1:
line = file.readline() #從文件中讀出一行
if not line: #如果讀出的是文件結尾,就退出循環
break
elif matchPattern.search(line): #如果讀出的行匹配上了定義的reg,那啥也不做
pass
else: #如果讀出的行不匹配reg,也不是文件尾,那就
list.append(line) #把這行存入list列表。
file.close()

file = open('target.txt', 'w') #重新打開一個文件,把list列表裡面的內容
for i in list: #逐行寫入。最後'target.txt'文件就是不包含
file.write(i) #'dana'那行的文件
file.close()

G. python 打開某個文件 刪除指定行

是不是想要直接打開文件夾,那使用如下命令就可以
import
os
os.system('explorer.exe
/n,
文件夾路徑')
這樣就可以直接打開,要打開文件,不知道你是想在程序里讀還是直接開文件,開文件用
os.system('cmd
/c
文件名')
腳本中使用文件內容,那就使用open函數來讀取文件內容。

H. python 刪除多個文本里的指定行

代碼基於python 2.6。功能已寫成函數,用的簡單語法,很好懂。
新文件文件名自動附加"_back"。不懂再問。

import os, time

def readKeys(fileName):
keys = []
f = open(fileName, "r")
while True:
line = f.readline()
if not line:
break
key = line.strip()
if key: keys.append(key)
f.close()
return keys

def processKeys(editFileName, backFileName, keys):
f = open(editFileName, "r")
lines = f.readlines()
f.close()

editLines = []
backLines = []

for line in lines:
found = False
for key in keys:
if line.startswith(key):
backLines.append(line)
found = True
break
if not found:
editLines.append(line)

if backLines:
f = open(editFileName, "w")
f.writelines(editLines)
f.close()
f = open(backFileName, "w")
f.writelines(backLines)
print 'modify',editFileName,'save',backFileName

if __name__ == '__main__':
keys = readKeys("0.txt")
fileList = ["1.txt", "2.txt", "3.txt", "4.txt", "5.txt"]
while True:
for fileName in fileList:
base, ext = os.path.splitext(fileName)
processKeys(fileName, base + "_back" + ext, keys)
print 'sleep 30 seconds'
time.sleep(30)

I. python 如何刪除excel 特定行

篩選--自定義篩選----選『不等於某值'---填入你所謂的特定字元----確定

J. python中怎麼刪除文件中指定的行

刪除文件的某一行,可以跳過你要刪除的行進行讀寫,如:

1
2
3
4

data = open(filename, 'rt').readlines()
with open(filename, 'wt') as handle:
handle.writelines(data[:tobedeleted])
handle.writelines(data[tobedeleted+1:])

其中data是逐行讀取文件,

handle.writelines進行讀寫,跳過tobedeleted行

熱點內容
crv哪個配置性價比高2021 發布:2024-09-17 04:07:51 瀏覽:35
wincc圖形編譯在哪裡 發布:2024-09-17 03:58:26 瀏覽:977
androidubuntu 發布:2024-09-17 03:50:27 瀏覽:701
識夢源碼 發布:2024-09-17 03:50:18 瀏覽:26
諾基亞密碼忘了打什麼電話 發布:2024-09-17 03:27:09 瀏覽:555
樹深度優先演算法 發布:2024-09-17 03:26:58 瀏覽:472
跳轉頁源碼 發布:2024-09-17 03:13:05 瀏覽:543
html文件上傳表單 發布:2024-09-17 03:08:02 瀏覽:785
聊天軟體編程 發布:2024-09-17 03:00:07 瀏覽:726
linuxoracle安裝路徑 發布:2024-09-17 01:57:29 瀏覽:688