str.translate给出TypeError - Translate接受一个参数(给定2个),在Python 2中工作

car*_*ear 49 python typeerror nltk

我有以下代码

import nltk, os, json, csv, string, cPickle
from scipy.stats import scoreatpercentile

lmtzr = nltk.stem.wordnet.WordNetLemmatizer()

def sanitize(wordList): 
answer = [word.translate(None, string.punctuation) for word in wordList] 
answer = [lmtzr.lemmatize(word.lower()) for word in answer]
return answer

words = []
for filename in json_list:
    words.extend([sanitize(nltk.word_tokenize(' '.join([tweet['text'] 
                   for tweet in json.load(open(filename,READ))])))])
Run Code Online (Sandbox Code Playgroud)

我写的时候,我在一个单独的testing.py文件中测试过2-4行

import nltk, os, json, csv, string, cPickle
from scipy.stats import scoreatpercentile

wordList= ['\'the', 'the', '"the']
print wordList
wordList2 = [word.translate(None, string.punctuation) for word in wordList]
print wordList2
answer = [lmtzr.lemmatize(word.lower()) for word in wordList2]
print answer

freq = nltk.FreqDist(wordList2)
print freq
Run Code Online (Sandbox Code Playgroud)

并且命令提示符返回['the','the','the'],这就是我想要的(删除标点符号).

但是,当我将完全相同的代码放在不同的文件中时,python会返回一个TypeError来说明

File "foo.py", line 8, in <module>
  for tweet in json.load(open(filename, READ))])))])
File "foo.py", line 2, in sanitize
  answer = [word.translate(None, string.punctuation) for word in wordList]
TypeError: translate() takes exactly one argument (2 given)
Run Code Online (Sandbox Code Playgroud)

json_list是所有文件路径的列表(我打印并检查此列表是否有效).我对这个TypeError很困惑,因为当我只是在另一个文件中测试时,一切都运行得很好.

drc*_*uck 90

如果你想要完成的只是做你在Python 3中用Python 2做的同样的事情,那么我在Python 2.0中做的就是抛弃标点符号和数字:

text = text.translate(None, string.punctuation)
text = text.translate(None, '1234567890')
Run Code Online (Sandbox Code Playgroud)

这是我的Python 3.0等价物:

text = text.translate(str.maketrans('','',string.punctuation))
text = text.translate(str.maketrans('','','1234567890'))
Run Code Online (Sandbox Code Playgroud)

基本上它表示"不做任何事情"(前两个参数)并将任何标点或数字转换为None(即删除它们).

  • 您可以通过使用`text.translate(str.maketrans('','',string.punctuation +'1234567890'))`甚至更好地使用`text.translate(str.maketrans()来组合这两个映射. '','',string.punctuation + string.digits))`.我首先将翻译地图存储在一个单独的常量中并重新使用它. (2认同)

Blc*_*ght 70

我怀疑你的问题是与之间的差异做str.translateunicode.translate(这些也是之间的差异str.translate上的Python 2与Python 3中).我怀疑您的原始代码是在发送unicode实例时,而您的测试代码是使用常规的8位str实例.

我不建议将Unicode字符串转换回常规str实例,因为unicode它是处理文本数据的更好的类型(它是未来!).相反,您应该适应新的unicode.translate语法.使用常规str.translate(在Python 2上),您可以传递一个可选deletechars参数,其中的字符将从字符串中删除.对于unicode.translate(以及str.translate在Python 3上),不再允许额外参数,但None将从输出中删除具有其值的转换表条目.

要解决此问题,您需要创建适当的转换表.转换表是从Unicode序数(即ints)到序数,字符串或的字典映射None.用于制作它们的辅助函数在Python 2中存在string.maketrans(和Python 3作为该str类型的方法),但它的Python 2版本不处理我们关心的情况(将None值放入表中).你可以自己用类似的东西建立一个合适的字典{ord(c): None for c in string.punctuation}.


小智 6

蟒蛇 3.0:

text = text.translate(str.maketrans('','','1234567890'))
Run Code Online (Sandbox Code Playgroud)

静态 str.maketrans(x[, y[, z]])

此静态方法返回可用于 的转换表str.translate()

如果只有一个参数,则它必须是将 Unicode 序数(整数)或字符(长度为 1 的字符串)映射到 Unicode 序数、字符串(任意长度)或None. 然后字符键将被转换为序数。

如果有两个参数,它们必须是等长的字符串,并且在生成的字典中,每个字符 inx都会映射到 in 中相同位置的字符y。如果有第三个参数,它必须是一个字符串,其字符将被映射到None结果中。

https://docs.python.org/3/library/stdtypes.html?highlight=maketrans#str.maketrans


小智 6

This is how translate works:

yourstring.translate(str.maketrans(fromstr, tostr, deletestr))
Run Code Online (Sandbox Code Playgroud)

Replace the characters in fromstr with the character in the same position in tostr and delete all characters that are in deletestr. The fromstr and tostr can be empty strings and the deletestr parameter can be omitted.

example:

str="preetideepak12345aeiou"
>>> str.translate(str.maketrans('abcde','12345','p'))
Run Code Online (Sandbox Code Playgroud)

output:

'r55ti4551k1234515iou'
Run Code Online (Sandbox Code Playgroud)

here:

a is translated to 1
b is translated to 2
c is translated to 3 and so on
and p is deleted from string.
Run Code Online (Sandbox Code Playgroud)