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