我有以下代碼:
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字符,但如果我只想洗掉包含在雙引號內的值,我不知道如何執行此操作。
這是我的嘗試:
我找到了這個,并使用了這個代碼:
my_text = re.sub(r'"\\n"','',my_text)
但它似乎不起作用。
uj5u.com熱心網友回復:
我可能有點復雜,但這樣的事情可能會奏效
parts = content.split("\"")
for i, part in enumerate(parts):
if i % 2:
parts[i] = part.replace("\n", "")
content = "\"".join(parts)
uj5u.com熱心網友回復:
弄清楚了。
腳步:
- 將位元組轉換為字串
- 為 Regex 創建模式
- 使用正則運算式來格式化值。
第1步:
my_text = my_text.decode("utf-8")
第2步:
pattern = re.compile(r'".*?"',re.DOTALL)
第 3 步:
my_text = pattern.sub(lambda x:x.group().replace('\n',''),my_text)
這解決了我的問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/315089.html
下一篇:Python從字串決議json
