Python 类型对象有问题';str';没有属性';maketrans';
这是我的代码:Python 类型对象有问题';str';没有属性';maketrans';,python,python-3.x,list,Python,Python 3.x,List,这是我的代码: fhand = open('romeo.txt') counts = dict() for line in fhand: line = line.translate(str.maketrans('', '', string.punctuation)) line = line.lower() words = line.split() for word in words: if word not in counts:
fhand = open('romeo.txt')
counts = dict()
for line in fhand:
line = line.translate(str.maketrans('', '', string.punctuation))
line = line.lower()
words = line.split()
for word in words:
if word not in counts:
counts[word] = 1
else:
counts[word] += 1
我不断得到错误:
第72行,在
line=line.translate(str.maketrans(“”,,,string.标点))
AttributeError:类型对象“str”没有属性“maketrans”
您确定使用的是Python 3吗?尝试导入系统;打印(sys.version\u info)您确定没有在该代码上方的某个位置为str
赋值,例如str=“test value”
之类吗?出于调试目的,您可以将print(repr(str),type(str))
放在导致错误的行之前。您确定使用的是Python 3吗?尝试导入系统;打印(sys.version\u info)您确定没有在该代码上方的某个位置为str
赋值,例如str=“test value”
之类吗?出于调试目的,您可以在导致错误的行之前放置print(repr(str),type(str))
。