假如我们有如下FASTA格式的文件,我们想把多行序列合并为一行,怎么做?
fh=open("G:/weiwei/创建文件/test2.fa","r")
for line in fh:
print(line)
>NM_001011874 gene=Xkr4 CDS=151-2091
gcggcggcgggcgagcgggcgctggagtaggagctggggagcggcgcggccggggaaggaagccagggcg
aggcgaggaggtggcgggaggaggagaca
gcagggacaggTGTCAGATAAAGGAGTGCTCTCCTCCGCTG
CCGAGGCATCATGGCCGCTAAGTCAGACGGGAGGCTGAAGATGAAGAAGAGCAGCGACGTGGCGTTCACC
CCGCTGCAGAACTCGGACAATTCGGGCTCTGTGCAAGGAC
TGGCTCCAGGCTTGCCGTCGGGGTCCGGAG
>NM_001195662 gene=Rp1 CDS=55-909
AAGCTCAGCCTTTGCTCAGATTCTCCTCTTGATGAAACAAAGGGATTTCTGCACATGCTTGAGAAATTGC
AGGTCTCACCCAAAATGAGTGACACA
CCTTCTACTAGTTTCTCCATGATTCATCTGACTTCTGAAGGTCA
AGTTCCTTCCCCTCGCCATTCAAATATCACTCATCCTGTAGTGGCTAAACGCATCAGTTTCTATAAGAGT
GGAGACCCACAGTTTGGCGGCGTTCG
GGTGGTGGTCAACCCTCGTTCCTTTAAGACTTTTGACGCTCTGC
TGGACAGTTTATCCAGGAAGGTACCC
CTGCCCTTTGGGGTAAGGAACATCAGCACGCCCCGTGGACGACA
CAGCATCACCAGGCTGGAGGAGCTAGAGGACGGCAAGTCTTATGTGTGCTCCCACAATAAGAAGGTGCTG
>NM_011283 gene=Rp1 CDS=128-6412
AATAAATCCAAAGACATTTGTTTACGTGAAACAAGCAGGTTGCATATCCAGTGACGTTTATACAGACCAC
ACAAACTATTTACTCTTTTCTTCGTAAGGAAAGGTTCAACTTCTGGTCTCACCCAAAATGAGTGACACAC
CTTCTACTAGTTTCTCCATGATT
CATCTGACTTCTGAAGGTCAAGTTCCTTCCCCTCGCCATTCAAATAT
CACTCATCCTGTAGTGGCTAAACGCATCAGTTTCTATAAGAGTGGAGACCCACAGTTTGGCGGCGTTCGG
GTGGTGGTCAACCCTCGTTCCTT
TAAGACTTTTGACGCTCTGCTGGACAGTTTATCCAGGAAGGTACCCC
TGCCCTTTGGGGTAAGGAACATCAGCACGCCCCGTGGACGACACAGCATCACCAGGCTGGAGGAGCTAGA
GGACGGCAAGTCTTATGTGTGCTC
CCACAATAAGAAGGTGCTGCCAGTTGACCTGGACAAGGCCCGCAGG
CGCCCTCGGCCCTGGCTGAGTAGTCGCTCCATAAGCACGCATGTGCAGCTCTGTCCTGCAACTGCCAATA
TGTCCACCATGGCACCTGGCATGCTCCGTGCCCCAAGGAGGCTCGTGGTCTTCCGGAATGGTGACCCGAA
>NM_0112835 gene=Rp1 CDS=128-6412
AATAAATCCAAAGACATTTGTTTACGTGAAACAAGCAGGTTGCATATCCAGTGACGTTTATACAGACCAC
ACAAACTATTTACTCTTTTCTTCGTA
AGGAAAGGTTCAACTTCTGGTCTCACCCAAAATGAGTGACACAC
CTTCTACTAGTTTCTCCATGATTCATCTGACTTCTGAAGGTCAAGTTCCTTCCCCTCGCCATTCAAATAT
CACTCATCCTGTAGTGGCTAAACGCATCAGTTTCTATAAGAGTGGAGACCCACAGTTTGGCGGCGTTCGG
GTGGTGGTCAACCCTCGTTCCTTTAA
GACTTTTGACGCTCTGCTGGACAGTTTATCCAGGAAGGTACCCC
TGCCCTTTGGGGTAAGGAACATCAGCACGCCCCGTGGACGACACAGCATCACCAGGCTGGAGGAGCTAGA
GGACGGCAAGTCTTATGTGTGCTCCCACAATAAGAAGGTGCTGCCAGTTGACCTGGACAAGGCCCGCAGG
CGCCCTCGGCCCTGGCTGAGTAGTCG
CTCCATAAGCACGCATGTGCAGCTCTGTCCTGCAACTGCCAATA
TGTCCACCATGGCACCTGGCATGCTCCGTGCCCCAAGGAG
GCTCGTGGTCTTCCGGAATGGTGACCCGAA
fh.close()
fh=open("G:/weiwei/创建文件/test2.fa","r")
a={}
for line in fh:
if line[0]==">":
key=line.strip()
a[key]=[]
else:
a[key].append(line.strip())
for key, valueL in a.items():
print(key)
print(''.join(valueL))
>NM_001011874 gene=Xkr4 CDS=151-2091
gcggcggcgggcgagcgggcgctggagtaggagctggggagcggcgcggccggggaaggaagccagggcgaggcgaggaggtggcgggaggaggagacagcagggacaggTGTCAGATAAAGGAGTGCTCTCCTCCGCTGCCGAGGCATCATGGCCGCTAAGTCAGACGGGAGGCTGAAGATGAAGAAGAGCAGCGACGTGGCGTTCACCCCGCTGCAGAACTCGGACAATTCGGGCTCTGTGCAAGGACTGGCTCCAGGCTTGCCGTCGGGGTCCGGAG
>NM_001195662 gene=Rp1 CDS=55-909
AAGCTCAGCCTTTGCTCAGATTCTCCTCTTGATGAAACAAAGGGATTTCTGCACATGCTTGAGAAATTGCAGGTCTCACCCAAAATGAGTGACACACCTTCTACTAGTTTCTCCATGATTCATCTGACTTCTGAAGGTCAAGTTCCTTCCCCTCGCCATTCAAATATCACTCATCCTGTAGTGGCTAAACGCATCAGTTTCTATAAGAGTGGAGACCCACAGTTTGGCGGCGTTCGGGTGGTGGTCAACCCTCGTTCCTTTAAGACTTTTGACGCTCTGCTGGACAGTTTATCCAGGAAGGTACCCCTGCCCTTTGGGGTAAGGAACATCAGCACGCCCCGTGGACGACACAGCATCACCAGGCTGGAGGAGCTAGAGGACGGCAAGTCTTATGTGTGCTCCCACAATAAGAAGGTGCTG
>NM_011283 gene=Rp1 CDS=128-6412
AATAAATCCAAAGACATTTGTTTACGTGAAACAAGCAGGTTGCATATCCAGTGACGTTTATACAGACCACACAAACTATTTACTCTTTTCTTCGTAAGGAAAGGTTCAACTTCTGGTCTCACCCAAAATGAGTGACACACCTTCTACTAGTTTCTCCATGATTCATCTGACTTCTGAAGGTCAAGTTCCTTCCCCTCGCCATTCAAATATCACTCATCCTGTAGTGGCTAAACGCATCAGTTTCTATAAGAGTGGAGACCCACAGTTTGGCGGCGTTCGGGTGGTGGTCAACCCTCGTTCCTTTAAGACTTTTGACGCTCTGCTGGACAGTTTATCCAGGAAGGTACCCCTGCCCTTTGGGGTAAGGAACATCAGCACGCCCCGTGGACGACACAGCATCACCAGGCTGGAGGAGCTAGAGGACGGCAAGTCTTATGTGTGCTCCCACAATAAGAAGGTGCTGCCAGTTGACCTGGACAAGGCCCGCAGGCGCCCTCGGCCCTGGCTGAGTAGTCGCTCCATAAGCACGCATGTGCAGCTCTGTCCTGCAACTGCCAATATGTCCACCATGGCACCTGGCATGCTCCGTGCCCCAAGGAGGCTCGTGGTCTTCCGGAATGGTGACCCGAA
>NM_0112835 gene=Rp1 CDS=128-6412
AATAAATCCAAAGACATTTGTTTACGTGAAACAAGCAGGTTGCATATCCAGTGACGTTTATACAGACCACACAAACTATTTACTCTTTTCTTCGTAAGGAAAGGTTCAACTTCTGGTCTCACCCAAAATGAGTGACACACCTTCTACTAGTTTCTCCATGATTCATCTGACTTCTGAAGGTCAAGTTCCTTCCCCTCGCCATTCAAATATCACTCATCCTGTAGTGGCTAAACGCATCAGTTTCTATAAGAGTGGAGACCCACAGTTTGGCGGCGTTCGGGTGGTGGTCAACCCTCGTTCCTTTAAGACTTTTGACGCTCTGCTGGACAGTTTATCCAGGAAGGTACCCCTGCCCTTTGGGGTAAGGAACATCAGCACGCCCCGTGGACGACACAGCATCACCAGGCTGGAGGAGCTAGAGGACGGCAAGTCTTATGTGTGCTCCCACAATAAGAAGGTGCTGCCAGTTGACCTGGACAAGGCCCGCAGGCGCCCTCGGCCCTGGCTGAGTAGTCGCTCCATAAGCACGCATGTGCAGCTCTGTCCTGCAACTGCCAATATGTCCACCATGGCACCTGGCATGCTCCGTGCCCCAAGGAGGCTCGTGGTCTTCCGGAATGGTGACCCGAA
字符串变量操作
print("字符串变量")
a = "Hello, welcome to Python"
#a = 123
#a = str(a)
print("The string a is:", a)
#占位符
print( "The length of this string <<%s>> is %d" % (a, len(a)))
# %s 代表字符串
# %d 代表的是数字
print("The type of a is", type(a))
字符串变量
The string a is: Hello, welcome to Python
The length of this string <<Hello, welcome to Python>> is 24
The type of a is <class 'str'>
a = "Hello, welcome to Python"
print("取出字符串的第一个字符、最后一个字符、中间部分字符")
print("The first character of a is %s\n" % a[0])
print("The first five characters of a are %s\n" % a[0:5])
print("The last character of a is %s\n" % a[-1])##取最后一个字符串的两种写法
print("The last character of a is %s\n" % a[len(a)-1]) ##取最后一个字符串的两种写法
print("\n这部分很重要啊,字符串的索引和切片操作是及其常用的。")
取出字符串的第一个字符、最后一个字符、中间部分字符
The first character of a is H
The first five characters of a are Hello
The last character of a is n
The last character of a is n
这部分很重要啊,字符串的索引和切片操作是及其常用的。
python 字符串—find函数
内置函数find只能确定最先出现的 该字母 的位置
print("我们看看用内置函数如何找到所有 o 的位置\n")
a = "oaoaoaoa"
print("内置函数find只能确定最先出现的 o 的位置")
pos = a.find('o')
print("因此,我们要在发现 o 之后,截取其后的字符串,再执行find操作")
while 1:
print(pos + 1)
new = a[pos+1:].find('o')
if new == -1:
break
pos = new + pos +1
#help(str)
我们看看用内置函数如何找到所有 o 的位置
内置函数find只能确定最先出现的 o 的位置
因此,我们要在发现 o 之后,截取其后的字符串,再执行find操作
1
3
5
7
a="oaoaoaoa"
#因为 index() 成员得到 list 中出现的第一个值就结束,所以对于上式,它每次返回的都是下标0
gg=[print(a.index(i)) for i in a if i=='o']
0
0
0
0
ggg=[print(i) for i in range(len(a)) if a[i]=='o']
0
2
4
6
利用split分割字符串,返回值为列表
print("利用split分割字符串\n")
str1 = "a b c d e f g"
strL = str1.split(' ')
print(strL)
print("\n使用split命令就可以把字符串分成列表了,想取用哪一列都随便你了。")
#使用下面的命令查看可以对字符串进行的操作
#help(str)
#利用split分割字符串"
利用split分割字符串
['a', 'b', 'c', 'd', 'e', 'f', 'g']
使用split命令就可以把字符串分成列表了,想取用哪一列都随便你了。
len(strL)
7
gh=[print(i) for i in strL]
a
b
c
d
e
f
g
+ 用来字符串的链接
a = "Hello"
b = "Python"
c = a + ', ' + b
print(c)
Hello, Python
strip()函数用来去除换行符,以及行末尾的空格 ##当然是默认情况下去除空格
a = "oneline\n"
print(len(a))
print("Currently, the string <a> is **", a, "**. \
\n The length of string <a> is **", len(a),"**. \
\n 我为什么换到下一行了?\n")
8
Currently, the string <a> is ** oneline
**.
The length of string <a> is ** 8 **.
我为什么换到下一行了?
b= a.strip()
print("Currently, the string <a> is **", b, "**. \
\n The length of string <a> is **", len(b),"**. \
\n 删掉了换行符后,少了个字符,而且没换行!\n")
Currently, the string <a> is ** oneline **.
The length of string <a> is ** 7 **.
删掉了换行符后,少了个字符,而且没换行!
c = a.strip('o') ###后面加上一个字符,表示去除该字符 且该字符必须是还有首字母
print(c)
neline
d = a.strip('on')
print(d)
eline
e = a.strip('n')
print(e)
oneline
replace()函数—字符串的替换
a = "Hello, Python"
b = a.replace("Hello","Welcome") ##第一个是要替换的对象,第二个是新的替换内容
print(b)
Welcome, Python
默认情况下,是全部替换
c = a.replace("o","O")
print(c)
HellO, PythOn
如果只想替换第一个字母,及第一个字母的小写o替换为大写O,该怎么办呢?
答案就是replace函数后面加一个位置参数,代表的是几个替换
##0个替换
c = a.replace("o","O",0)
print(c)
Hello, Python
##1个替换
c = a.replace("o","O",1)
print(c)
HellO, Python
##2个替换
c = a.replace("o","O",2)
print(c)
HellO, PythOn
a = "忙处事为,常向闲中先检点,过举自稀;动时念想,预从静里密操持,非心自息。"
print(a.replace(';','\n'))
忙处事为,常向闲中先检点,过举自稀
动时念想,预从静里密操持,非心自息。
大小写转换 a.lower()/ a.upper()
a = 'Sdsdsd'
print("All elements in <%s> is lowercase: %s" % (a, a.islower()))
print("Transfer all elments in <%s> to lowerse <%s>" % (a, a.lower()))
print("Transfer all elments in <%s> to upperse <%s>" % (a, a.upper()))
All elements in <Sdsdsd> is lowercase: False
Transfer all elments in <Sdsdsd> to lowerse <sdsdsd>
Transfer all elments in <Sdsdsd> to upperse <SDSDSD>
a.upper()
'SDSDSD'
a
'Sdsdsd'
a.lower()
'sdsdsd'