如何从Python2.7中的Unicode字符串中删除\r、\n、\t ?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (446)

我得到了一些数据,里面充满了转义字符:

{"website": "http://www.zebrawebworks.com/zebra/bluetavern/day.cfm?&year=2018&month=7&day=10", "headliner": ["\"Roda Vibe\" with the Tallahassee Choro Society"], "data": [" \r\n    ", "\r\n\t\r\n\r\n\t", "\r\n\t\r\n\t\r\n\t", "\r\n\t", "\r\n\t", "\r\n\t", "8:00 PM", "\r\n\t\r\n\tFEE:  $2 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ", "\r\n\tEvery 2nd & 4th Tuesday of the month, the Choro Society returns to Blue Tavern with that subtly infectious Brazilian rhythm and beautiful melodies that will stay with you for days. The perfect antidote to Taylor Swift. $2 for musicians; tips appreciated. ", "\r\n\t", "\r\n\t\r\n\t", "\r\n\t", "\r\n\t", "\r\n\t\r\n\t\r\n\r\n\t\r\n\t", "\r\n\t\r\n\t\t", "\r\n", "\r\n", "\r\n", "\r\n"]},

我试图编写一个函数来删除这些字符,但我的两种策略都不起作用:

    # strategy 1
    escapes = ''.join([chr(char) for char in range(1, 32)])
    table = {ord(char): None for char in escapes}
    for item in concert['data']:
        item = item.translate(table)
    # strategy 2
    for item in concert['data']:
        for char in item:
            char = char.replace("\r", "").replace("\t", "").replace("\n", "")
提问于
用户回答回答于

以下为原因:

lst = ["aaa", "abc", "def"]

for x in lst:
    x = x.replace("a","z")

print(lst)  # ['aaa', 'abc', 'def']

列表似乎没有变化, (重新)为for循环中使用的变量赋值(x)工程内循环,但是更改永远不会传播回lst

与此相反:

for (i,x) in enumerate(lst):
    lst[i] = x.replace("a","z")

print(lst)  # ['zzz', 'zbc', 'def']

for i in range(len(lst)):
    lst[i] = lst[i].replace("a","z")

print(lst)  # ['zzz', 'zbc', 'def']

扫码关注云+社区

领取腾讯云代金券