a=''.maketrans
时间: 2024-02-02 12:02:38 浏览: 21
这是一个字符串方法,用于创建字符映射表。其中,a为一个空字符串,调用该方法时需要传递两个参数,分别为映射的源字符集和目标字符集。返回值为一个映射表,可用于后续字符串的转换。比如,执行语句"table = ''.maketrans('abc', '123')"将创建一个将字符'a'映射为'1'、'b'映射为'2'、'c'映射为'3'的映射表table。
相关问题
# coding=utf-8 import string def translator(frm='', to='', delete='', keep=None): if len(to) == 1: to = to * len(frm) trans = string.maketrans(frm, to) # string.maketrans设置字符串转换规则表,aTob = string.maketrans('a','b')#将字符a转换为字符b if keep is not None: allchars = string.maketrans('', '') delete = allchars.translate(allchars, keep.translate(allchars, delete)) # translate函数进行字符串的替换和删除 # translate(字符串转换规则表,要删除的字符串) # https://www.qyyshop.com/info/432490.html def translate(s): return s.translate(trans, delete) return translate return translator print (digits_only('Chris Perkins: 224-7992')) # 指定字符替换数字 digits_to_hash = translator(frm=string.digits, to='#') print 22222222222222,digits_to_hash # 如果为基本数据类型的话,经常会使用type(函数) # 如果为基本对象类型的话,经常会使用dir(函数) # object <>: print (digits_to_hash('Chris Perkins:224-7992')) 以上代码出现以下异常:ValueError: maketrans arguments must have same length 应该如何解决
出现该异常是因为`maketrans()`函数的两个参数的长度不一致。在你的代码中,`frm`和`to`的长度不一致,导致了该异常。
要解决这个问题,你需要确保`frm`和`to`的长度相同。你可以使用以下方法来生成一个长度相同的字符串来替代`to`:
```python
to = to * len(frm)
```
将以上代码添加到你的代码中,即可解决该异常。修正后的代码如下所示:
```python
import string
def translator(frm='', to='', delete='', keep=None):
if len(to) == 1:
to = to * len(frm)
trans = string.maketrans(frm, to)
if keep is not None:
allchars = string.maketrans('', '')
delete = allchars.translate(allchars, keep.translate(allchars, delete))
def translate(s):
return s.translate(trans, delete)
return translate
return translator
print(translator(string.digits, '#')('Chris Perkins: 224-7992'))
```
运行修正后的代码,将会输出`Chris Perkins: ###-####`,其中数字已被替换为`#`。
table = str.maketrans('', '', string.punctuation)
This line of code creates a translation table that can be used to remove all punctuation marks from a string. Specifically, it uses the string method `maketrans()` to create a translation table that maps each punctuation mark to None (i.e. it removes them from the string). The first two arguments of `maketrans()` are empty strings, which means that no characters will be replaced by other characters. The third argument, `string.punctuation`, is a predefined string in the `string` module that contains all punctuation marks.
This translation table can be passed to the `translate()` method of a string to remove all punctuation marks. For example:
```
text = "Hello, world!"
clean_text = text.translate(table)
print(clean_text) # Output: "Hello world"
```
Here, `table` is the translation table created by `maketrans()`, and `clean_text` is the original text with all punctuation marks removed.
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)