Python - Remove extended ascii

2024/10/12 14:20:21

Okay, so I am new to the whole python world so bear with me.

Background: We are trying to offload logs into mongo to be able to query and search for them quicker. The device already prints them in a decent format EXCEPT in between each }{ to begin and end the data object something like this:

¾ïúÀï{"id":"xxx","timestamp":xxx,"payloadType":"xxx","payload":{"protocol":"xxx","zoneID":xxx,"zoneName":"xxx","eventType":"xxx"}}’ÂCº¾ïúÀï{"id":"xxx","timestamp":xxx,"payloadType":"xxx","payload":{"protocol":"xxx","zoneID":xxx,"zoneName":"xxx","eventType":"xx}}

Using the following I've been able to convert it to bytes then back to a string which outputs as:

f = open('logfile', 'r')
file_data = f.read()
f.close()data = file_data.encode('utf-8')print(str(data))>>>b'\xc2\xbe\xc3\xaf\xc3\xba\xc3\x80\x01\xc3\xaf{"id":"xxx","timestamp":xxx,"payloadType":"xxx","payload":{"protocol":"xxx","zoneID":xxx,"zoneName":"xxx","eventType":"xxx"}}

In my mind this is easier to deal with than the ugly characters seen above but I don't know.

This is just a sample..there are thousands upon thousands of lines returned in this log. In my mind, ideally the best way to go about this would be to remove all characters at the beginning of the string before { and all characters in between }}{

Answer

Encode the string to bytes and then decode back to ASCII:

data.encode().decode('ascii',errors='ignore')
# {"id":"xxx","timestamp":xxx,...}}

You can also use regular expressions to remove all characters outside of the outermost curly braces:

re.sub(r'^[^{]*(?={)|(?<=})[^}{]*(?={)|(?<=})[^}]*$', '', data)

The latter mechanism incidentally also removes the ASCII 'C' character that you do not want.

https://en.xdnf.cn/q/118190.html

Related Q&A

Selenium - Python - Select dropdown meun option - No ID or Name

I am trying to select and element in a dropdown menu:The HTML is:<div class="col-lg-6"><select data-bind="options: indicator_type_list,value: indicatorType,optionsCaption: Choos…

How to prevent triples from getting mixed up while uploading to Dydra programmatically?

I am trying to upload some data to Dydra from a Sesame triplestore I have on my computer. While the download from Sesame works fine, the triples get mixed up (the s-p-o relationships change as the obje…

Adding a new row to a dataframe in pandas for every iteration

Adding a new row to a dataframe with correct mapping in pandasSomething similar to the above question.carrier_plan_identifier ... hios_issuer_identifier 1 AU…

Twisted client protocol - attaching an interface frontend

I am following the tutorial on writing a client/server pair in Twisted located here:http://twistedmatrix.com/documents/current/core/howto/clients.htmlI have everything working for the communication of …

Get query string as function parameters on flask

Is there a way to get query string as function parameters on flask? For example, the request will be like this.http://localhost:5000/user?age=15&gender=MaleAnd hope the code similar to this.@app.…

cython.parallel cannot see the difference in speed

I tried to use cython.parallel prange. I can only see two cores 50% being used. How can I make use of all the cores. i.e. send the loops to the cores simultaneously sharing the arrays, volume and mc_vo…

Is it possible (how) to add a spot color to pdf from matplotlib?

I am creating a chart which has to use (multiple) spot colors. This color could be one that is neither accessible from RGB nor CMYK. Is there a possibility to specify a spot color for a line in matplot…

Separate keywords and @ mentions from dataset

I have a huge set of data which has several columns and about 10k rows in more than 100 csv files, for now I am concerned about only one column with message format and from them I want to extract two p…

Kivy class in .py and .kv interaction 2

Follow up from Kivy class in .py and .kv interaction , but more complex. Here is the full code of what Im writing: The data/screens/learnkanji_want.kv has how I want the code to be, but I dont fully un…

How to centre an image in pygame? [duplicate]

This question already has an answer here:How to center an image in the middle of the window in pygame?(1 answer)Closed 1 year ago.I am using python 2.7, I would like to know if there is a way to centr…