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

时间:2017-10-16 10:28:38

标签: python django python-2.7 django-1.10

在我的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)

2 个答案:

答案 0 :(得分:2)

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

接下来 - translateunicode的{​​{1}}函数采用不同的参数 - 你需要一个string来将Unicode序列映射到序数或Unicode字符串, dict仅适用于maketrans翻译。

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

试试这个:

ord

答案 1 :(得分:1)

你可以像这样手动完成:

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。