第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

ValueError: 轉(zhuǎn)換表的長度必須為 256 個(gè)字符

ValueError: 轉(zhuǎn)換表的長度必須為 256 個(gè)字符

慕妹3242003 2022-07-05 19:04:19
我有 python 燒瓶項(xiàng)目(程序),我將用戶輸入的帶有大寫字母和標(biāo)點(diǎn)符號的字符串轉(zhuǎn)換為沒有它們的字符串。當(dāng)我運(yùn)行程序時(shí),我收到以下錯(cuò)誤:ValueError: translation table must be 256 characters long Traceback (most recent call last) File:"/home/sd18656/.local/lib/python2.7/site-packages/flask/app.py", line2463, in __call__ return self.wsgi_app(environ, start_response) File"/home/sd18656/.local/lib/python2.7/site-packages/flask/app.py", line2449, in wsgi_app response = self.handle_exception(e) File"/home/sd18656/.local/lib/python2.7/site-packages/flask/app.py", line1866, in handle_exception reraise(exc_type, exc_value, tb) File"/home/sd18656/.local/lib/python2.7/site-packages/flask/app.py", line2446, in wsgi_app response = self.full_dispatch_request() File"/home/sd18656/.local/lib/python2.7/site-packages/flask/app.py", line1951, in full_dispatch_request rv = self.handle_user_exception(e) File"/home/sd18656/.local/lib/python2.7/site-packages/flask/app.py", line1820, in handle_user_exception reraise(exc_type, exc_value, tb) File"/home/sd18656/.local/lib/python2.7/site-packages/flask/app.py", line1949, in full_dispatch_request rv = self.dispatch_request() File"/home/sd18656/.local/lib/python2.7/site-packages/flask/app.py", line1935, in dispatch_request returnself.view_functions[rule.endpoint](**req.view_args) File"/home/sd18656/FlaskProject/mainapp.py", line 61, in home score,total_processed_data = get_score(abstract) File"/home/sd18656/FlaskProject/mainapp.py", line 32, in get_scoreabstract = abstract.translate(string.punctuation).lower()在程序中,abstract 是一個(gè)字符串類型。我遇到了這個(gè)解決方案:string.translate 函數(shù)中的“表”是什么意思?但是,對于該錯(cuò)誤,orstring.maketrans似乎并不順利。我該如何解決這個(gè)問題?lower()upper()
查看完整描述

1 回答

?
三國紛爭

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超7個(gè)贊

您需要將轉(zhuǎn)換表傳遞給str.translate(string.translate在 Python 2 中)。翻譯表無非是一個(gè)dictwherekey是搜索字符并且value是替換字符, wherekey和value是ord()各個(gè)字符的 the。


例如,如果您想punctuation用空格替換所有字符,請執(zhí)行以下操作:


from string import punctuation

transtable = string.maketrans(punctuation, ' ' * len(punctuation))


abstract = abstract.translate(transtable).lower()

這是一個(gè)概念證明:


>>> from string import punctuation

>>> transtable = string.maketrans(punctuation, ' ' * len(punctuation))

>>> type(transtable)

<class 'dict'>

>>> 'This!is#a.string,with;punctuations:'.translate(transtable).lower()

'this is a string with punctuations '

>>> 

這是maketrans和的另一個(gè)用法示例translate(只是為了好玩):


>>> elite = string.maketrans('aeiou', '4310v')

>>> 'Hackers Rulez'.translate(elite)

'H4ck3rs Rvl3z'

>>> 


查看完整回答
反對 回復(fù) 2022-07-05
  • 1 回答
  • 0 關(guān)注
  • 241 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號