本文介绍了类型错误:translate() 只需要一个参数(给出 2 个)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码在 python 2.x 版本上运行良好,但是当我尝试在 python 3.x 版本上运行它时,它给出了错误.

主题:需要对短信编码中的任何消息进行缩写.

代码:

def sms_encoding(data):#开始在这里编写代码打印(数据)数据.split(" ")data_list=data.split(" ")sms_encd=[]final_sms=""对于范围内的 i(len(data_list)):如果 data_list[i].lower() 在 ['a','e','i','o','u']:sms_encd.append(data_list[i])elif len(data_list[i])>1:a = data_list[i].translate(None,'aeiouAEIOU')sms_encd.append(a)对于范围内的 j(len(sms_encd)):final_sms += str(sms_encd[j])+" "返回 final_sms[:-1]数据="我不会重复错误"打印(短信编码(数据))

输出:

2.x 版本:

我不会重复错误我不会 rpt mstks

3.x 版本:

我不会重复错误回溯(最近一次调用最后一次): 中的文件python",第 18 行文件python",第 12 行,在 sms_encoding 中类型错误:translate() 只需要一个参数(给出 2 个)

为什么 translate() 不起作用?有什么替代的解决方法吗?

解决方案

你需要比较 Python 3 的 str.translate() 和 Python 2 的 unicode.translate().两者都从代码点(一个整数)到替换(另一个整数或单字符 Unicode e 字符串)进行映射.

str 类型有一个静态方法 str.maketrans() 将要删除的字符(Python 2 的 str.translate() 的第二个参数)作为第三个参数,以制作这样的地图.在这里使用它:

map = str.maketrans('', '', 'aeiouAEIOU')a = data_list[i].translate(map)

这会输出一个字典,将每个元音代码点映射到 None:

>>>str.maketrans('', '', 'aeiouAEIOU'){97:无,101:无,105:无,111:无,117:无,65:无,69:无,73:无,79:无,85:无}

My code is running fine on python 2.x versions but when I'm trying to run it on python 3.x version, it's giving error.

subject: need to abbreviate any message in sms encoding.

Code:

def sms_encoding(data):
    #start writing your code here
    print(data)
    data.split(" ")
    data_list=data.split(" ")
    sms_encd=[]
    final_sms=""
    for i in range(len(data_list)):
        if data_list[i].lower() in  ['a','e','i','o','u']:
            sms_encd.append(data_list[i])
        elif len(data_list[i])>1:
            a = data_list[i].translate(None,'aeiouAEIOU')
            sms_encd.append(a)
    for j in range(len(sms_encd)):
        final_sms += str(sms_encd[j])+" "
    return final_sms[:-1]
data="I will not repeat mistakes"
print(sms_encoding(data))

Output:

2.x versions:

I will not repeat mistakes
I wll nt rpt mstks

3.x versions:

I will not repeat mistakes
Traceback (most recent call last):
  File "python", line 18, in <module>
  File "python", line 12, in sms_encoding
TypeError: translate() takes exactly one argument (2 given)

why translate() is not working? is there any alternative workaround?

解决方案

You need to compare Python 3's str.translate() with Python 2's unicode.translate(). Both take a mapping from codepoint (an integer) to a replacement (either another integer or a single-character Unicode e string).

The str type has a static method str.maketrans() that takes the characters-to-delete (the second argument to Python 2's str.translate()) as the third argument, to produce such a map. Use that here:

map = str.maketrans('', '', 'aeiouAEIOU')
a = data_list[i].translate(map)

This outputs a dictionary mapping each of the vowel codepoints to None:

>>> str.maketrans('', '', 'aeiouAEIOU')
{97: None, 101: None, 105: None, 111: None, 117: None, 65: None, 69: None, 73: None, 79: None, 85: None}

这篇关于类型错误:translate() 只需要一个参数(给出 2 个)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 01:04