[英]str.translate gives TypeError - Translate takes one argument (2 given), worked in Python 2
我有以下代碼
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))])))])
我在編寫時在單獨的 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
並且命令提示符返回 ['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)
json_list 是所有文件路徑的列表(我打印並檢查此列表是否有效)。 我對這個 TypeError 感到困惑,因為當我只是在不同的文件中測試它時,一切都很好。
如果您想要完成的只是在 Python 3 中做與在 Python 2 中所做的相同的事情,那么我在 Python 2.0 中所做的就是丟棄標點符號和數字:
text = text.translate(None, string.punctuation)
text = text.translate(None, '1234567890')
這是我的 Python 3.0 等效項:
text = text.translate(str.maketrans('','',string.punctuation))
text = text.translate(str.maketrans('','','1234567890'))
基本上它說“什么都不翻譯”(前兩個參數)並將任何標點符號或數字翻譯為“ None
(即刪除它們)。
我懷疑您的問題與str.translate
和unicode.translate
之間的差異unicode.translate
(這些也是 Python 2 與 Python 3 上的str.translate
之間的差異)。 我懷疑您的原始代碼正在發送unicode
實例,而您的測試代碼正在使用常規 8 位str
實例。
我不建議將 Unicode 字符串轉換回常規str
實例,因為unicode
是處理文本數據更好的類型(而且它是未來!)。 相反,您應該只適應新的unicode.translate
語法。 使用常規str.translate
(在 Python 2 上),您可以傳遞一個可選的deletechars
參數,並且其中的字符將從字符串中刪除。 對於unicode.translate
(以及 Python 3 上的str.translate
),不再允許使用額外的參數,但是將從輸出中刪除以None
作為其值的轉換表條目。
要解決這個問題,您需要創建一個適當的轉換表。 轉換表是從 Unicode 序數(即int
s)到序數、字符串或None
的字典映射。 在 Python 2 中存在一個用於生成它們的輔助函數string.maketrans
(Python 3 作為str
類型的方法),但它的 Python 2 版本不處理我們關心的情況(將None
值放入表中) )。 你可以自己構建一個合適的字典,比如{ord(c): None for c in string.punctuation}
。
蟒蛇 3.0:
text = text.translate(str.maketrans('','','1234567890'))
靜態 str.maketrans(x[, y[, z]])
這個靜態方法返回一個可用於
str.translate()
的轉換表。
如果只有一個參數,則它必須是將 Unicode 序數(整數)或字符(長度為 1 的字符串)映射到 Unicode 序數、字符串(任意長度)或None
的字典。 然后字符鍵將被轉換為序數。
如果有兩個參數,它們必須是等長的字符串,並且在生成的字典中, x
每個字符都將映射到y
相同位置的字符。 如果有第三個參數,它必須是一個字符串,其字符將在結果中映射為None
。
https://docs.python.org/3/library/stdtypes.html?highlight=maketrans#str.maketrans
這是翻譯的工作方式:
yourstring.translate(str.maketrans(fromstr, tostr, deletestr))
替換字符fromstr
與在同一位置的字符tostr
並刪除在所有字符deletestr
。 fromstr
和tostr
可以為空字符串,可以省略deletestr
參數。
例:
str="preetideepak12345aeiou"
>>> str.translate(str.maketrans('abcde','12345','p'))
輸出:
'r55ti4551k1234515iou'
這里:
a is translated to 1
b is translated to 2
c is translated to 3 and so on
and p is deleted from string.
如果你只是想實現這樣的東西: "123hello.jpg".translate(None, 0123456789")
然后試試這個:
"".join(c for c in "123hello.jpg" if c not in "0123456789")
輸出:你好.jpg
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.