導航:首頁 > 編程語言 > python找出指定字元並輸出

python找出指定字元並輸出

發布時間:2022-07-20 03:00:09

A. python中找到特定的字元並輸出到另一個文件中

importre
fname='a.txt'
withopen(fname)asf:
s=f.read()
mwp=re.findall(r'([0-9.e+]+)s*#s*MWP',s)
width=re.findall(r's34s+([0-9.e+]+)',s)

B. python尋找特定字元附近的內容並輸出

正則表達式:[^\x00-\xff]|\d+[^\x00-\xff]

C. Python如何輸出某關鍵字元並輸出完整字元串

如果存在序列佔用多行的情況:

importitertools

deffile2dict(filename):
name=""
content=""
dct={}
forlninitertools.ifilter(None,itertools.imap(
lambdas:s.strip(),
open(filename,'rt')
)):
ifln.startswith(">"):
ifcontent:
dct[name]=content
name,content=ln[1:],""
else:
content+=ln
dct[name]=content
returndct

datadict=file2dict("SEQ.FASTA")
print"Total%dserial."%len(datadict)

D. python如何在字元串中查找指定字元並報出位置

%不太清楚題目,我按理解寫了一段你試試看
test=str(bin(1770)[2:])
output=[]
for i in test:

output.append[i]

print output
%下面會得出indices
for i in enumerate[test]:
print i

E. python如何實現分行提取指定字元串

python讀取文件內容的方法:一.最方便的方法是一次性讀取文件中的所有內容並放置到一個大字元串中:all_the_text=open('thefile.txt').read()#文本文件中的所有文本all_the_data=open('abinfile','rb').read()#二進制文件中的所有數據為了安全起見,最好還是給打開的文件對象指定一個名字,這樣在完成操作之後可以迅速關閉文件,防止一些無用的文件對象佔用內存。舉個例子,對文本文件讀取:file_object=open('thefile.txt')try:all_the_text=file_object.read()finally:file_object.close()不一定要在這里用Try/finally語句,但是用了效果更好,因為它可以保證文件對象被關閉,即使在讀取中發生了嚴重錯誤。二.最簡單、最快,也最具Python風格的方法是逐行讀取文本文件內容,並將讀取的數據放置到一個字元串列表中:list_of_all_the_lines=file_object.readlines()這樣讀出的每行文本末尾都帶有"\n"符號;如果你不想這樣,還有另一個替代的辦法,比如:list_of_all_the_lines=file_object.read().splitlines()list_of_all_the_lines=file_object.read().split('\n')list_of_all_the_lines=[L.rstrip('\n')forLinfile_object]

F. Python讀取一個字元串,並循環輸出幾遍

總共六個字元
python可以通過str*2重復輸出字元串printstr*2#輸出字元串兩次,循環代碼塊就是將取到的每個字元進行列印輸出,總共六個字元,就循環執行了6次。接下來我們使用For循環遍歷一個列表。

G. python中,如何輸出選定的字元串

代碼如下:

a="走路回家"

b="跑步回家"

r=input("請輸入a或b:")

if r=="a":print(a)

elif r=="b":print(b)

else:print("原地不動")

運行結果截圖如下:

H. python 查找字元串 並輸出整行

示例代碼:

注意事項:

test.txt文本格式為UTF-8。

I. python將字元串的數字取出並輸出成一個新的字元串

將字元串a中ascii碼值在(47,58)區間里的字元(即字元0-9)提取出來組成一個字元串然後輸出

J. python 搜索一個指定txt 的字元串 並輸出其下面兩行

fromtkinterimport*
fromtkinterimportttk
fromtkinterimportfont
importos
importarrange

#課程的字典
crslst={}
crslst_in_num={}

#GUI
root=Tk()

#frame2

#右側的內容
frame2=ttk.Frame(root,borderwidth=5,relief="sunken")
frame2.grid(column=1,row=0)

#將數字轉換為漢字的字典
numtoch={1:'一',2:'二',3:'三',4:'四',5:'五'}

#設置課表周圍周一至周五和1至13的標簽
fornuminlist(range(1,6)):
ttk.Label(frame2,text='周'+numtoch[num]).grid(column=num,row=0)
emptylist=[]
fornuminlist(range(1,14)):
ttk.Label(frame2,text='第'+str(num)+'節').grid(column=0,row=num)

#課表主體
schd1=Listbox(frame2,height=13)
schd1.grid(column=1,row=1,rowspan=13,sticky=(N,S,E,W))
schd2=Listbox(frame2,height=13)
schd2.grid(column=2,row=1,rowspan=13,sticky=(N,S,E,W))
schd3=Listbox(frame2,height=13)
schd3.grid(column=3,row=1,rowspan=13,sticky=(N,S,E,W))
schd4=Listbox(frame2,height=13)
schd4.grid(column=4,row=1,rowspan=13,sticky=(N,S,E,W))
schd5=Listbox(frame2,height=13)
schd5.grid(column=5,row=1,rowspan=13,sticky=(N,S,E,W))

#設置字體
ft=font.Font(family='MicrosoftYaHei',size=12,weight='normal')

#為了便捷將五天的課表作為一list
daylist=(schd1,schd2,schd3,schd4,schd5)

#設置課表
fordayindaylist:
day['selectmode']='extended'
day['exportselection']=False
fornuminrange(1,14):
day.insert(0,'無課')
day['font']=ft
day['width']=10


#課表下四個按鈕和一個combobox
#改變顏色
defchcolor():
print("33[1;42m33[0m")


butdont=ttk.Button(frame2,text='不上課',command=chcolor).grid(column=3,row=15)

#清除選擇
butusel=ttk.Button(frame2,text='清除選擇').grid(column=4,row=15)
Label(frame2,text='選擇課表').grid(column=0,row=15,columnspan=2,sticky=W)
outputlist=ttk.Combobox(frame2,textvariable=['待選','ok'])
outputlist.grid(column=1,row=15,columnspan=2,sticky=E)

#frame1
#左側的內容
frame1=ttk.Frame(root,borderwidth=5,relief="sunken")
frame1.grid(column=0,row=0)

#設置上方的label
Label(frame1,text='輸入課程信息').grid(column=0,row=0,columnspan=2)

#輸入課程號的Label和entry
Label(frame1,text='課程號:').grid(column=0,row=1,sticky=W)
coursenum=StringVar()
inputcoursenum=ttk.Entry(frame1,textvariable=coursenum,width=13)
inputcoursenum.grid(column=1,row=1,sticky=E)

#已選課程的listbox
Label(frame1,text='已選課程').grid(column=0,row=3,sticky=W)
courselist=Listbox(frame1,height=8,width=13,exportselection=False)
courselist.grid(column=0,row=4,columnspan=2,rowspan=8,sticky=W)


defisrtcrs():#加入課程
k=0
num=0
print(num,courselist.size())
whilenum<courselist.size():
ifinputcoursenum.get()==courselist.get(num):
k=1
num+=1
ifk==0:
courselist.insert(0,inputcoursenum.get())
crslst[inputcoursenum.get()]=[]
crslst_in_num[inputcoursenum.get()]=[]
print(crslst)
print(crslst_in_num)
return


defdelcrs():#刪除課程
#print(courselist.curselection())
#print(courselist.get(courselist.curselection()))
crslst_in_num.pop(courselist.get(courselist.curselection()))
crslst.pop(courselist.get(courselist.curselection()))
courselist.delete(courselist.curselection())
print(crslst)
print(crslst_in_num)
return


defshwtm(*args):#當選中一門課程,時間listbox改為對應的時間
print('here')
timelist.delete(0,END)
tmlst=crslst[courselist.get(courselist.curselection())]
iflen(tmlst)!=0:
fortimeintmlst:
timelist.insert(END,time)
return


#courselist的選中事件
courselist.bind('<<ListboxSelect>>',shwtm)

#加入課程和刪除課程的按鈕
addcourse=ttk.Button(frame1,text='加入課程',command=isrtcrs).grid(column=0,row=2)
deletcourse=ttk.Button(frame1,text='刪除課程',command=delcrs).grid(column=0,row=12)

#已選課程的時間listbox
Label(frame1,text='時間列表').grid(column=1,row=3,sticky=E)
timelist=Listbox(frame1,height=8,width=13,exportselection=False)
timelist.grid(column=1,row=4,columnspan=2,rowspan=8,sticky=E)


defisrttim():#加入時間
num=1
time=[]
time_in_num=[]
strofod=''
fordayindaylist:
iflen(day.curselection())!=0:
strofod=numtoch[num]
temp=day.curselection()
forclsintemp:
strofod=strofod+''+str(cls+1)
time_in_num.append((num-1)*13+(cls+1))
num+=1
iflen(strofod)!=0:
time.append(strofod)
strofod=[]
iflen(time)!=0:
timelist.insert(END,time)
crslst[courselist.get(courselist.curselection())].append(time)
crslst_in_num[courselist.get(courselist.curselection())].append(time_in_num)
print(crslst)
print(crslst_in_num)
return


defdeltim():#刪除時間
print(timelist.curselection())
forselintimelist.curselection():
crslst[courselist.get(courselist.curselection())].pop(sel)
crslst_in_num[courselist.get(courselist.curselection())].pop(sel)
timelist.delete(timelist.curselection())
print(crslst)
print(crslst_in_num)
return


#加入時間和刪除時間的按鈕
addtime=ttk.Button(frame1,text='加入時間',command=isrttim).grid(column=1,row=2)
delelttime=ttk.Button(frame1,text='刪除時間',command=deltim).grid(column=1,row=12)


defcreatcourselist():#建立課程列文本文檔
withopen('slist.txt','w')asslist:
slist.writelines(str(len(crslst_in_num))+' ')#課程數
forcrs,tlstincrslst_in_num.items():
slist.writelines(crs+' ')#課程名
slist.writelines(str(len(tlst))+' ')#可選時間數
fortimeintlst:
slist.writelines(str(len(time))+' ')#某一可選時間的節數
fornintime:
slist.writelines(str(n)+' ')#第幾節
return


defshowoutput():#在右側的選擇課表combobox里顯示可選課表
filename_list=[]
forfilename_einos.listdir('output'):
filename_list.append(filename_e)
outputlist['values']=filename_list
return


defarrg():#排課
print('here')
creatcourselist()
forfilename_einos.listdir('output'):
os.remove('output/'+filename_e)
arrange.arrangement_mod()
showoutput()
return


#當用戶選擇某一可選課表時,展示課表
defshowcrslist(*args):
print(outputlist.get())
forfilename_einos.listdir('output'):#尋找所選項對應的文件
iffilename_e==outputlist.get():
withopen('output/'+filename_e,'r')asf_r:#打開對應的文件
nr=f_r.readlines()#按行讀取內容
k=0
fordayindaylist:
day.delete(0,END)#刪除課表的原有內容
fornuminrange(1,14):
#print(nr[k])
#加入新的內容
ifnr[k]=='0 ':
day.insert(END,'')
else:
day.insert(END,nr[k].strip())
k+=1
break#完成之後退出循環
return


#可選課表combobox選擇事件
outputlist.bind('<<ComboboxSelected>>',showcrslist)

#排課的按鈕
run=ttk.Button(frame1,text='排課',command=arrg).grid(column=0,row=13,columnspan=2)

#frame_search
frame_s=ttk.Frame(root,borderwidth=5,relief='sunken')
frame_s.grid(column=2,row=0)
ttk.Label(frame_s,text='搜索課程').grid(column=0,row=0,columnspan=2)
ttk.Label(frame_s,text='課程號:').grid(column=0,row=1,sticky=E)

crs_num=StringVar()
crs_search=ttk.Entry(frame_s,textvariable=crs_num,width=12).grid(column=1,row=1,sticky=E)

search_crs=ttk.Button(frame_s,text='搜索').grid(column=0,row=2)
adp=ttk.Button(frame_s,text='加入').grid(column=1,row=2)

ttk.Label(frame_s,text='課程號:').grid(column=0,row=3,columnspan=2,sticky=W)
ttk.Label(frame_s,text='課程名:').grid(column=0,row=4,columnspan=2,sticky=W)
ttk.Label(frame_s,text='上課時間:').grid(column=0,row=5,columnspan=2,sticky=W)

root.mainloop()

閱讀全文

與python找出指定字元並輸出相關的資料

熱點內容
android自動連接指定wifi 瀏覽:491
用紙做超簡單又解壓的東西 瀏覽:596
國密2演算法是對稱的嗎 瀏覽:465
nc65伺服器地址配置 瀏覽:522
單片機實驗報告電子琴 瀏覽:744
程序員恢復微信文件代碼 瀏覽:517
有漁python 瀏覽:81
pdf字體加深 瀏覽:206
怎麼做一個minecraft伺服器 瀏覽:771
c語言實現ls命令 瀏覽:663
小洋糕解壓視頻 瀏覽:450
域名內網訪問內網伺服器地址 瀏覽:138
我的世界伺服器如何摳掉金幣 瀏覽:223
域名與ip地址通過什麼伺服器查 瀏覽:96
企業網站需要什麼雲伺服器配置 瀏覽:910
遼事通伺服器出現錯誤是什麼原因 瀏覽:766
能否將一個表格的子表加密 瀏覽:64
手機ios微信收藏怎麼加密 瀏覽:594
安卓如何改黑色 瀏覽:331
oracle資料庫導出命令 瀏覽:697