當前位置:首頁 » 編程語言 » python指定行

python指定行

發布時間: 2022-02-27 07:47:17

python 讀取指定行數

F=('n'.join(open('C:\Users\Administrator\Desktop\ID.txt','r',encoding='gbk').readlines()[b:c]))

② python如何指定寫入內容的行數,以及如何讀取指定行數(已知)

不用關心行數。
只要把要保存的變數,用字典組織起來。然後用str()行數轉成字元串存到文件中。
讀取的時候,只要將讀取的字元串,用eval()行數,再轉回字典,然後按key進行區分,分別還給變數就可以了。

③ 用python修改文件中指定的行數

python默認庫不能隨機讀寫文件。只能一次讀入和寫入。以下是示例代碼。

filename='setup.ini'
lines=[]
withopen(filename,'r',encoding='utf8')asf:
lines=f.readlines()
lines[167]='UPI1=Chinese'
lines[231]='UPI1=Chinese'
withopen(filename,'w',encoding='utf8')asf:
f.write(' '.join(lines))
f.flush()

修改需謹慎,修改前請記得備份,不明白可追問。

④ python open怎麼指定行插入

指定行插入比較麻煩,我一般會讀取文件,然後將讀到的內容一行行的寫入到一個新文件,以前回答的一個問題:https://..com/question/2121566381126811667

#-*-coding:utf-8-*-


#原始文件
f=open('d:\000001.csv','r+')
#新文件
t=open('d:\000002.txt','w+')

n=1
x=100
#讀取100行,插入一條數據
foriinf.readlines():
t.write(i)
ifn%100==0:
t.write('-----------------------%s----------------- '%x)
n=n+1

f.close()
t.close()

⑤ python怎麼剪切指定行

import re
r=re.compile(r'指定內容')
f0=file('01.txt','r')
dat0=f0.readlines()
f0.close()
f1=file('02.txt','w')
for i in dat0:
if r.search(i):
f1.write(i)
f1.close()

⑥ 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行

⑦ 如何用python讀取文本中指定行的內容

1.默認你知道「指定行」的行號
那麼:
defappoint_line(num,file):
withopen(file,"r",encoding='utf-8')asf:
out=f.readlines[num-1]
returnout
print(appoint_line(2,"c:/text.txt"))
以上示例為讀取c盤下的text.txt文件的第二行
2.假如所謂「指定行」為開頭幾個字元,這里假設為三個
defappoint_line(file):
#appoimt_spring是指你指定行的前三個字元,你可以自行指定
appoint_spring=input(">>").strip()
withopen(file,"r",encoding='utf-8')asf:
forlineinf.readlines():
ifline[0:3]==appoint_spring:
returnline
print(appoint_line("c:/text.txt"))
以上示例為根據你輸入的所指定行的前三個字元列印出c盤下的text.txt文件下的「指定行」

⑧ python怎麼讀取指定目錄、指定文件、指定行的值呢 麻煩回答的時候舉個例子

對於文件,python通常是無法讀取指定行的。不過是可以進行"曲線救國",但是這僅對文本文件生效,對於二進制文件,本身是沒有行的概念的,討論也沒意義,下面是一種可能的解決方案。

path='c:\documents'
filename='readme.txt'
fullfilename='%s\%s'%(path,filename)
defgetContentByRowNumber(rownumber,filehandle):
oldfilePos=filehandle.tell()
i=0
filehandle.seek(0)
whilei<rownumber:
l=filehandle.readline()
ifnotl:
returnNone
content=filehandle.readline()
filehandle.seek(oldfilePos)
returncontent
f=open(filename,'rt')
print(getContentByRowNumber(2,f))

⑨ Python怎麼跳到指定行

Python不像C有goto語句。

跳不了的。

建議用while循環


while True:

# 執行功能性代碼

# 判斷條件

if True:

break


這樣,就能實現符合代碼條件時繼續,不符合時再次運行功能性代碼

相當於跳轉到此功能性代碼。

熱點內容
安卓十二系統什麼時候更新 發布:2025-07-02 00:12:28 瀏覽:344
shell腳本需要編譯鏈接 發布:2025-07-02 00:04:20 瀏覽:473
微信如何重設密碼 發布:2025-07-02 00:02:27 瀏覽:545
java代碼基礎 發布:2025-07-02 00:00:46 瀏覽:304
煙花的代碼c語言 發布:2025-07-01 23:56:04 瀏覽:224
安卓默認打開文件方式怎麼修改 發布:2025-07-01 23:30:38 瀏覽:862
壓縮機接線座 發布:2025-07-01 23:17:48 瀏覽:662
iqoo瀏覽器緩存路徑 發布:2025-07-01 23:12:38 瀏覽:691
明日之後如何獲得最新伺服器 發布:2025-07-01 23:12:35 瀏覽:51
tv加密頻道 發布:2025-07-01 23:10:58 瀏覽:625