AttributeError:'str'对象没有属性'maketrans'

时间:2015-12-18 06:58:41

标签: python

def complement(dna):
    transtable = dna.maketrans('acgtnACGTN', 'tgcanTGCAN')
    return dna.translate(transtable)

import string
dna = raw_input("Enter DNA sequence: ")
print "Reverse Complement is: ", complement(dna)

我已经检查了dir(字符串)并且没有maketrans。 有没有办法导入maketrans?

1 个答案:

答案 0 :(得分:4)

这适用于Python 3.4:

def complement(dna):
    transtable = dna.maketrans('acgtnACGTN', 'tgcanTGCAN')
    return dna.translate(transtable)

print(complement('TGA'))

这适用于Python 2.7:

from __future__ import print_function
import string

def complement(dna):
    transtable = string.maketrans('acgtnACGTN', 'tgcanTGCAN')
    return dna.translate(transtable)

print(complement('TGA'))

这为您提供了运行脚本的主要Python版本:

import sys

print(sys.version_info.major)