gpt4 book ai didi

python - 如何将文本中的字母更改为其他字母 | Python 2.7?

转载 作者:行者123 更新时间:2023-11-30 22:27:51 25 4
gpt4 key购买 nike

在我的 Django 项目中,我有带有字段(“标题”)的表单。在此字段中,用户需要用俄语书写,但用户可能会错误地书写一些拉丁字母。我想改变他们。我使用PYTHON 2.7。下一个代码引发错误。如何修复这个错误?

from string import maketrans

eng = 'ETOPAHKXCBMetopahkxcbm' # English letters
rus = 'ЕТОРАНКХСВМеторанкхсвм' # Russian letters

def form_valid(self, form):
form.cleaned_data['title'].translate(maketrans(dict(zip(eng, rus))))
form.save()

错误:

Traceback (most recent call last):
File "/srv/envs/py27/lib/python2.7/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/srv/envs/py27/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/srv/envs/py27/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/srv/envs/py27/lib/python2.7/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/srv/envs/py27/lib/python2.7/site-packages/django/views/generic/base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
File "/srv/envs/py27/lib/python2.7/site-packages/django/views/generic/edit.py", line 217, in post
return super(BaseCreateView, self).post(request, *args, **kwargs)
File "/srv/envs/py27/lib/python2.7/site-packages/django/views/generic/edit.py", line 183, in post
return self.form_valid(form)
File "/home/nurzhan/dashboard.kase.kz/static_pages/views.py", line 54, in form_valid
form.cleaned_data['title'].translate(maketrans(dict(zip(eng, rus))))
TypeError: maketrans() takes exactly 2 arguments (1 given)

当我仅使用 form.cleaned_data['title'].translate(maketrans(eng, rus)) 时,它会引发错误:

Internal Server Error: /static_page/create/
Traceback (most recent call last):
File "/srv/envs/py27/lib/python2.7/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/srv/envs/py27/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/srv/envs/py27/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/srv/envs/py27/lib/python2.7/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/srv/envs/py27/lib/python2.7/site-packages/django/views/generic/base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
File "/srv/envs/py27/lib/python2.7/site-packages/django/views/generic/edit.py", line 217, in post
return super(BaseCreateView, self).post(request, *args, **kwargs)
File "/srv/envs/py27/lib/python2.7/site-packages/django/views/generic/edit.py", line 183, in post
return self.form_valid(form)
File "/home/nurzhan/dashboard.kase.kz/static_pages/views.py", line 54, in form_valid
form.cleaned_data['title'].translate(maketrans(eng, rus))
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-21: ordinal not in range(128)

最佳答案

如果您在代码中也使用 Unicode 文字,事情会变得更容易一些,这样您就不必担心 strunicode 类型问题。这与将所有字符串文字写为 u'stuff' 而不仅仅是 'stuff' 相同。

下一步 - unicodestringtranslate 函数采用不同的参数 - 您需要一个 dict unicode 将 Unicode 序数映射到序数或 Unicode 字符串,不是 maketrans,后者仅用于 str 翻译。

您可以使用内置的 ord 从 Unicode 字符中获取序数。

试试这个:

from __future__ import unicode_literals  # Put this at the top of your source

eng = 'ETOPAHKXCBMetopahkxcbm' # English letters
rus = 'ЕТОРАНКХСВМеторанкхсвм' # Russian letters

table = dict(zip(map(ord, eng), map(ord, rus)))
assert eng.translate(table) == rus # Verifying translation

关于python - 如何将文本中的字母更改为其他字母 | Python 2.7?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46768159/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com