如何将文本中的字母更改为其他字母 | Python2.7?

How to change letters in text to other letters | Python 2.7?

在我的 Django 项目中,我有带字段的表单 ("title")。在此字段中,用户需要用俄语书写,但用户可能会误写一些拉丁字母。我想改变他们。我使用 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 字符串, 不是 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

您可以像这样手动完成:

a_string = u"abcdeFghijklmn" # Let say it is cyrillic with one latin letter represented here by 'F' (uppercase)
# To change it to cyrillic i.e. here to lower latin you would do:
l2c = {u"F": u"f"} # Map all latin to cyrillic for real
l2c_get = l2c.get # Faster access but not necessary
a_string = u"".join(
    l2c_get(x, x) for x in a_string)

当然,你可以使用 unicode.translate() 方法,它应该做同样的事情。

当您的 'a_string' 被处理后,只需将其推回 django。所有映射到西里尔字母的拉丁字母都将被替换。请注意映射中的所有内容都是 unicode。如果将翻译后的字符串放入 Django 会引发 UnicodeDecode/Encode 错误,这意味着您应该在尝试之前:

a_string = a_string.encode("utf-8", "ignore")

应该接受二进制的 UTF-8。