移除专用通道

2024-04-26 09:46:06 发布

您现在位置:Python中文网/ 问答频道 /正文

如何将输入文本'abcde'f gh'转换为输出'abcdefgh'?你知道吗

这不管用。你知道吗

a='abcde'f gh'
b=a.translate({(u"\u0027"):None})

Tags: 文本noneghtranslateabcdeabcdefghu0027
1条回答
网友
1楼 · 发布于 2024-04-26 09:46:06

您应该转义撇号'或使用引号"来定义字符串:

>> a='abcde\'f gh'

或者

>> a="abcde'f gh"

要删除符号'和空格,请按如下方式使用string.translate

>> b = a.translate(None," \'")
'abcdefgh'

string.translate(s, table[, deletechars])

Delete all characters from s that are in deletechars (if present), and then translate the characters using table, which must be a 256-character string giving the translation for each character value, indexed by its ordinal. If table is None, then only the character deletion step is performed.

相关问题 更多 >