【问题标题】:migrate str.translate from Python 2 to Python 3将 str.translate 从 Python 2 迁移到 Python 3
【发布时间】:2015-08-19 18:10:48
【问题描述】:

如果传递给它的字符串是二进制的,我正在尝试编写一个函数以返回 True。如何在 Python 3.4.3 下运行这个 Python 2.x 代码?这是我得到的错误:

TypeError: translate() takes exactly one argument (2 given)

代码:

textchars = bytearray([0,7,8,9,10,12,13,27]) + bytearray(range(0x20, 0x100))
is_binary_string = lambda data: bool(data.translate(None,textchars))

我阅读了 str.maketrans() 的文档,但不明白如何将其合并到代码中。

【问题讨论】:

  • “是二进制”是什么意思?
  • this 对您有帮助吗?

标签: python


【解决方案1】:

在 Python 3.4 中,您不再拥有 deletechars 参数,而是可以将映射字典传递给 str.translate() ,并且对于要删除的所有元素,您可以使用空字符串 '' 作为值。在您的情况下,代码将变为 -

from itertools import zip_longest
textchars = bytearray([0,7,8,9,10,12,13,27]) + bytearray(range(0x20, 0x100))
textdict = dict(zip_longest(textchars,[''],fillvalue=''))
is_binary_string = lambda data: bool(data.translate(textdict))

上面的代码会将空字符串分配给 textchars 中的所有元素,因为这也是您想要的。


示例/演示 -

>>> textchars = bytearray([0,7,8,9,10,12,13,27]) + bytearray(range(0x20, 0x100))
>>> from itertools import zip_longest
>>> textdict = dict(zip_longest(textchars,[''],fillvalue=''))
>>> pprint.pprint(textdict)
{0: '',
 7: '',
 8: '',
 9: '',
 10: '',
 12: '',
 13: '',
 27: '',
 32: '',
 33: '',
 34: '',
 35: '',
 36: '',
 37: '',
 38: '',
 39: '',
 40: '',
 41: '',
 42: '',
 43: '',
 44: '',
 45: '',
 46: '',
 47: '',
 48: '',
 49: '',
 50: '',
 51: '',
 52: '',
 53: '',
 54: '',
 55: '',
 56: '',
 57: '',
 58: '',
 59: '',
 60: '',
 61: '',
 62: '',
 63: '',
 64: '',
 65: '',
 66: '',
 67: '',
 68: '',
 69: '',
 70: '',
 71: '',
 72: '',
 73: '',
 74: '',
 75: '',
 76: '',
 77: '',
 78: '',
 79: '',
 80: '',
 81: '',
 82: '',
 83: '',
 84: '',
 85: '',
 86: '',
 87: '',
 88: '',
 89: '',
 90: '',
 91: '',
 92: '',
 93: '',
 94: '',
 95: '',
 96: '',
 97: '',
 98: '',
 99: '',
 100: '',
 101: '',
 102: '',
 103: '',
 104: '',
 105: '',
 106: '',
 107: '',
 108: '',
 109: '',
 110: '',
 111: '',
 112: '',
 113: '',
 114: '',
 115: '',
 116: '',
 117: '',
 118: '',
 119: '',
 120: '',
 121: '',
 122: '',
 123: '',
 124: '',
 125: '',
 126: '',
 127: '',
 128: '',
 129: '',
 130: '',
 131: '',
 132: '',
 133: '',
 134: '',
 135: '',
 136: '',
 137: '',
 138: '',
 139: '',
 140: '',
 141: '',
 142: '',
 143: '',
 144: '',
 145: '',
 146: '',
 147: '',
 148: '',
 149: '',
 150: '',
 151: '',
 152: '',
 153: '',
 154: '',
 155: '',
 156: '',
 157: '',
 158: '',
 159: '',
 160: '',
 161: '',
 162: '',
 163: '',
 164: '',
 165: '',
 166: '',
 167: '',
 168: '',
 169: '',
 170: '',
 171: '',
 172: '',
 173: '',
 174: '',
 175: '',
 176: '',
 177: '',
 178: '',
 179: '',
 180: '',
 181: '',
 182: '',
 183: '',
 184: '',
 185: '',
 186: '',
 187: '',
 188: '',
 189: '',
 190: '',
 191: '',
 192: '',
 193: '',
 194: '',
 195: '',
 196: '',
 197: '',
 198: '',
 199: '',
 200: '',
 201: '',
 202: '',
 203: '',
 204: '',
 205: '',
 206: '',
 207: '',
 208: '',
 209: '',
 210: '',
 211: '',
 212: '',
 213: '',
 214: '',
 215: '',
 216: '',
 217: '',
 218: '',
 219: '',
 220: '',
 221: '',
 222: '',
 223: '',
 224: '',
 225: '',
 226: '',
 227: '',
 228: '',
 229: '',
 230: '',
 231: '',
 232: '',
 233: '',
 234: '',
 235: '',
 236: '',
 237: '',
 238: '',
 239: '',
 240: '',
 241: '',
 242: '',
 243: '',
 244: '',
 245: '',
 246: '',
 247: '',
 248: '',
 249: '',
 250: '',
 251: '',
 252: '',
 253: '',
 254: '',
 255: ''}
>>> 'abcdef'.translate(textdict)
''

如果数据是一个字节串,那么你仍然可以使用 `Python 2.x here - Example -

>>> textchars = bytearray([0,7,8,9,10,12,13,27]) + bytearray(range(0x20, 0x100))
>>> b'abcdefasdas'.translate(None,textchars)
b''

【讨论】:

  • 我在is_binary_string = 线上看到:TypeError: 'dict' does not support the buffer interface
  • 在使用 translate 之前,您需要将字节字符串解码为字符串。在 Python 3.x 中,普通字符串是 unicode 字符串,而在 Python 2.x 中,普通字符串是字节串。
  • 解码使用 - data.decode('utf-8') 或任何你喜欢的编码
  • 另外,如果您拥有的是字节字符串,那么您可以使用以前的 Python 2 方法本身,正如我在回答中更新的那样。
  • 我尝试使用 utf-8、latin-1 进行 decode(),但没有成功。我肯定传入了str 类型。我得到同样的错误。
猜你喜欢
  • 2019-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多