简体   繁体   English

AWS Lambda Python函数删除尾随的Unicode

[英]AWS Lambda Python function remove trailing unicode

I'm trying to use a Python 3.6 AWS Lambda function to parse Windows logs sent from Cloudwatch. 我正在尝试使用Python 3.6 AWS Lambda函数来解析从Cloudwatch发送的Windows日志。

These arrive in Lambda as JSON, so I extract the field I want using: 这些以JSON的形式到达Lambda,因此我使用以下方法提取想要的字段:

for i in data['logEvents']
   message = json.dumps((i['message']))

which gives me this in my message string: 这使我在我的message字符串:

"<Event xmlns='http://schemas.microsoft.com/win/2004/08/events/event'><System><Provider Name='Lfsvc'/><EventID Qualifiers='0'>2</EventID><Level>4</Level><Task>0</Task><Keywords>0x80000000000000</Keywords><TimeCreated SystemTime='2018-04-03T15:33:57.186213000Z'/><EventRecordID>25371</EventRecordID><Channel>System</Channel><Computer>EC2AMAZ-1KJC0H1</Computer><Security/></System><EventData></EventData><RenderingInfo Culture='en-US'><Message>Geolocation positioning has been disabled by the user.</Message><Level>Information</Level><Task></Task><Opcode>Info</Opcode><Channel></Channel><Provider></Provider><Keywords><Keyword>Classic</Keyword></Keywords></RenderingInfo></Event>\u0000"

I am then trying to turn this string into XML to use with either xmltodict or xml.etree.ElementTree, but both of those return a malformed XML error because of the \ at the end. 然后,我尝试将此字符串转换为XML以与xmltodict或xml.etree.ElementTree一起使用,但是由于结尾处\ ,这两个都返回格式错误的XML错误。

So I then run this to remove the unicode: 因此,我然后运行此命令以删除unicode:

xml = re.sub(u'(\u0000)', "", message)

which works fine on my computer in my local python console, and bothxmltodict & xml.etree.ElementTree can then work with the newly created xml string. 可以在本地python控制台上的计算机上正常工作,然后xmltodict和xml.etree.ElementTree都可以使用新创建的xml字符串。

But when I run the re.sub command in the Lambda function, the \ remains at the end of the string. 但是,当我在Lambda函数中运行re.sub命令时, \仍保留在字符串的末尾。

Am I missing something obvious?? 我缺少明显的东西吗?

Adding the full output of print(i['message']) 添加print(i['message'])的完整输出

<Event xmlns='http://schemas.microsoft.com/win/2004/08/events/event'><System><Provider Name='Service Control Manager' Guid='{555908d1-a6d7-4695-8e1e-26931d2012f4}' EventSourceName='Service Control Manager'/><EventID Qualifiers='16384'>7036</EventID><Version>0</Version><Level>4</Level><Task>0</Task><Opcode>0</Opcode><Keywords>0x8080000000000000</Keywords><TimeCreated SystemTime='2018-04-03T15:31:31.854941100Z'/><EventRecordID>25365</EventRecordID><Correlation/><Execution ProcessID='712' ThreadID='4768'/><Channel>System</Channel><Computer>EC2AMAZ-1KJC0H1</Computer><Security/></System><EventData><Data Name='param1'>Volume Shadow Copy</Data><Data Name='param2'>running</Data><Binary>5600530053002F0034000000</Binary></EventData><RenderingInfo Culture='en-US'><Message>The Volume Shadow Copy service entered the running state.</Message><Level>Information</Level><Task></Task><Opcode></Opcode><Channel></Channel><Provider>Microsoft-Windows-Service Control Manager</Provider><Keywords><Keyword>Classic</Keyword></Keywords></RenderingInfo></Event>\u0000

Many thanks, Dave 非常感谢,戴夫

You're issue might be caused by the use of json.dumps on the message string before doing re.sub . 您的问题可能是由于在执行re.sub之前在消息字符串上使用json.dumps引起的。 See example below: 请参见下面的示例:

import re, json

msg = "</Keywords></RenderingInfo></Event>\u0000"
xml_1 = re.sub(u'(\u0000)', "", msg)
print(xml_1)

message = json.dumps(msg)
xml_2 = re.sub(u'(\u0000)', "", message)
print(xml_2)

Output 输出量

</Keywords></RenderingInfo></Event>
"</Keywords></RenderingInfo></Event>\u0000"

As you can see in the outputs it seems like that json.dumps introduces a double-quote to the message which causes a problem with your re.sub 如您在输出中所看到的,似乎json.dumps在消息中引入了双引号 ,这导致您的re.sub问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM