簡體   English   中英

無法使用python re.sub從文本中刪除確切的字符串

[英]Unable to remove the exact string from text with python re.sub

我有以下文字:

{
'inputbuffer': 'x06x00x00x00ExplorerStartMenuReadyx00',  
'devicehandle': '0x0000033c', 
'controlcode': 2228388, 
'outputbuffer': 'Ŝx1b3Ϝx83)蝸11\x84ط°\x02򜸹2��Ѕ\x01A\x81wM\x9c4ø_󄜸-1@:b3.Ϝx#8?3)蝸11\x84ط°\x02򜸹2\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00', 
'function': 'openfile' 
}

我想替換以下部分:

'inputbuffer': 'x06x00x00x00ExplorerStartMenuReadyx00'

'inputbuffer':

'outputbuffer': 'Ŝx1b3Ϝx83)蝸11\x84ط°\x02򜸹2��Ѕ\x01A\x81wM\x9c4ø_󄜸-1@:b3.Ϝx#8?3)蝸11\x84ط°\x02򜸹2\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

'outputbuffer':

我寫了以下python代碼:

import codecs
import base64
x1="{'inputbuffer': 'x06x00x00x00ExplorerStartMenuReadyx00',  'devicehandle': '0x0000033c', 'controlcode': 2228388, 'outputbuffer': 'Ŝx1b3Ϝx83)蝸11\x84ط°\x02򜸹2��Ѕ\x01A\x81wM\x9c4ø_󄜸-1@:b3.Ϝx#8?3)蝸11\x84ط°\x02򜸹2\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00', 'function': 'openfile' }"
x3=re.sub(r'(^\w+)','',x1)
x4=re.sub(r'(\<|>)','',x3)
x5=re.sub(r'[^\x00-\x7F]+','', x4)
x6=re.sub(r'(\$|%|\|\(|\)|\\|@|\.|_|-|#|\?)','',x5)
x9=re.sub(r'\'outputbuffer\':\s\'.*\'','\'outputbuffer\':',x6, flags=re.IGNORECASE)
x10=re.sub(r'\'inputbuffer\':\s\'.*\',\s','\'inputbuffer\':',x9, flags=re.MULTILINE)
print(x10)

所需的輸出應僅替換這兩個部分,並保持其余部分不變,如下所示:

{'inputbuffer':,  'devicehandle': '0x0000033c', 'controlcode': 2228388, 'outputbuffer': }

但是我得到的是:

{'inputbuffer':'controlcode': 2228388, 'outputbuffer': }

刪除了一些應保留在結果文本中的部分。

如果有人幫助我弄清楚這段代碼有什么問題,我將非常感激。

不要將json轉換為文本,就可以很容易地實現目標。 只需使用此代碼

x1 = {'inputbuffer': 'x06x00x00x00ExplorerStartMenuReadyx00',  'devicehandle': '0x0000033c', 'controlcode': 2228388, 'outputbuffer': 'Ŝx1b3Ϝx83)蝸11\x84ط°\x02򜸹2��Ѕ\x01A\x81wM\x9c4ø_󄜸-1@:b3.Ϝx#8?3)蝸11\x84ط°\x02򜸹2\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00', 'function': 'openfile' }
x1[inputbuffer] = ""
x2[outputbuffer] = ""
print(x1)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM