Python学习

【Python生信编程笔记】读取文件

2023-06-11  本文已影响0人  生信开荒牛

在Python中,读取文件分为三个步骤:

一、打开文件:open

file_handle=open('readme.txt','r')
file_handle
<_io.TextIOWrapper name='readme.txt' mode='r' encoding='cp936'>

文件句柄不是文件,而是对文件的说明。

二、读取文件:有几个方法来读取文件。

使用read()来读取seqA.fas

file_handle=open('./samples/seqA.fas','r')
file_handle.read()
'>O00626|HUMAN Small inducible cytokine A22.\nMARLQTALLVVLVLLAVALQATEAGPYGANMEDSVCCRDYVRYRLPLRVVKHFYWTSDSCPRPGVVLLTFRDKEICADPRVPWVKMILNKLSQ\n'

三、关闭文件

不使用with

file_handle=open('readme.txt','r')
file_handle.read()
file_handle.close()

使用with

with open('readme.txt','r') as file_handle:
    file_handle.read()

四、实例

读取FASTA文件

with open('./samples/seqA.fas','r') as fh:
    print(fh.read())
>O00626|HUMAN Small inducible cytokine A22.
MARLQTALLVVLVLLAVALQATEAGPYGANMEDSVCCRDYVRYRLPLRVVKHFYWTSDSCPRPGVVLLTFRDKEICADPRVPWVKMILNKLSQ
  1. 将FASTA文件的名字和序列分开
with open('./samples/seqA.fas','r') as fh:
    my_file=fh.read()
name=my_file.split('\n')[0][1:]  #去掉>
sequece=''.join(my_file.split('\n')[1:]) 
print('The name is : {0}'.format(name))
print('The sequece is : {0}'.format(sequece))
The name is : O00626|HUMAN Small inducible cytokine A22.
The sequece is : MARLQTALLVVLVLLAVALQATEAGPYGANMEDSVCCRDYVRYRLPLRVVKHFYWTSDSCPRPGVVLLTFRDKEICADPRVPWVKMILNKLSQ
  1. 使用for进行遍历:
sequce=''
with open('./samples/seqA.fas','r') as fh:
    name=fh.readline()[1:-1]
    for line in fh:
        sequce+=line.replace('\n','')
print('The name is : {0}'.format(name))
print('The sequece is : {0}'.format(sequce))
The name is : O00626|HUMAN Small inducible cytokine A22.
The sequece is : MARLQTALLVVLVLLAVALQATEAGPYGANMEDSVCCRDYVRYRLPLRVVKHFYWTSDSCPRPGVVLLTFRDKEICADPRVPWVKMILNKLSQ
  1. 计算净电荷
sequence=''
charge=-0.002
aa_charge = {'C':-.045, 'D':-.999, 'E':-.998, 'H':.091,
'K':1, 'R':1, 'Y':-.001}
with open('./samples/prot.fas') as fh:
    #print(fh.read())
    fh.readline()
    for line in fh:
        #print(line[:-1])
        sequence+=line[:-1].upper()  #去掉\n
for aa in sequence:
    charge+=aa_charge.get(aa,0)
print(charge)
3.046999999999999
上一篇下一篇

猜你喜欢

热点阅读