【发布时间】:2021-10-14 02:26:58
【问题描述】:
我有以下代码:
output = requests.get(url=url, auth=oauth, headers=headers, data=payload)
output_data = output.content
type(output_date)
<class 'bytes'>
output_data
压缩文本(3632 行)
查看压缩后的文本时,我有一些看起来像这样的值:
Steve likes to walk his dog. Steve says to John "I like \n Pineapple, oranges, \n and pizza.\n" and then he went to bed \n.
John likes his beer cold.\n
Sally likes her teeth brushed with a bottle of jack.\n
如何删除 \n 字符,但前提是它包含在双引号内,以便我的结果如下所示:
Steve likes to walk his dog. Steve says to John "I like Pineapple, oranges, and pizza." and then he went to bed \n.
John likes his beer cold.\n
Sally likes her teeth brushed with a bottle of jack.\n
我知道如何删除 \n 字符,但如果我只想删除包含在双引号中的值,我不确定如何执行此操作。
这是我尝试过的:
我找到了this,并使用了这个代码:
my_text = re.sub(r'"\\n"','',my_text)
但它似乎不起作用。
【问题讨论】:
-
'"\\n"'您的文本不包含直接用双引号括起来的换行符。 -
你能澄清一下吗?我不明白。
-
re.sub(r'"\\n"','',my_text)这不起作用,因为它会查找双引号的精确模式,然后是换行符,然后是双引号。您的文本不包含该模式——它在双引号和换行符之间有多余的字符。
标签: python python-3.x newline