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("