How to replace \\ with \ without raising an EOL error?

2024/10/7 8:22:51

I am reading from a file that contains byte data but when I open the file and store the readline data into a variable it stores it in a string with backslash escapes, So when trying to decode that data I am getting the exact string data and not actually decoding.

File Example:

b'\xe0\xa8\xaa\xe0\xa9\xb0\xe0\xa8\x9c\xe0\xa8\xbe\xe0\xa8\xac\xe0\xa9\x80'
b'\xd9\xbe\xd9\x86\xd8\xac\xd8\xa7\xd8\xa8\xdb\x8c'
b'\xd9\xbe\xda\x9a\xd8\xaa\xd9\x88'

readline returns:

"b'\\xe0\\xa8\\xaa\\xe0\\xa9\\xb0\\xe0\\xa8\\x9c\\xe0\\xa8\\xbe\\xe0\\xa8\\xac\\xe0\\xa9\\x80'"

I get why there is an extra backslash, but I don't know how to remove it or read the file without it.

I have tried to replace those double backslashes but that raises an EOL error.

Answer

To convert the string representation of a bytes object to an actual bytes object, you could use ast.literal_eval().

>>> s = "b'\\xe0\\xa8\\xaa'"
>>> import ast
>>> b = ast.literal_eval(s)
>>> b
b'\xe0\xa8\xaa'
>>> b.decode('utf-8')
'ਪ'

Although, why does the file contain Python representations of bytes objects in the first place? Like, where is it coming from? If you're the one creating it, why not use the bytes themselves? Or you could at least use a standard serialization format like JSON.

Note that if the source of the file is untrusted and you use this approach, an attacker could crash your Python interpreter.

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

Related Q&A

How to find duplicates in pandas dataframe

Editing. Suppose I have the following series in pandas:>>>p 0 0.0 1 0.0 2 0.0 3 0.3 4 0.3 5 0.3 6 0.3 7 0.3 8 1.0 9 1.0 10 1.0 11 0.2 12 0.2 1…

i have error eol while scanning string literal

i dont know what is the problem im junior on python programer what happened on my code i study but i dnt understand this #fungsi coveragedef coverage ():print("[1] Kota Besar)print("[2] Kota…

How to extract specific data from JSON?

I cant seem to extract specific data from JSON which I retrieved from a link. I wrote this code and seems to work fine up to x [print(x) that is] as you can see from the screenshot-1. But, its giving e…

python csv: getting subset

here is a snapshot of my csv:alex 123f 1 harry fwef 2 alex sef 3 alex gsdf 4 alex wf35 6 harry sdfsdf 3i would like to get the subset of this data where the occurrence of a…

Variable within a Variable in Python (3)

My head is probably in the wrong place with this, but I want to put a variable within a variable.My goal for this script is to compare current versions of clients software with current software version…

selenium scraping data using children of elements

Hi im trying to scrape some data from a live stocks website. I want to display the companies name and stock price, %change ect. The details of 25 companies are shown per page, and these details follow …

Python - ETFs Daily Data Web Scraping

Im trying to web scrape some daily info of differents ETFs. I found that https://www.marketwatch.com/ have a accurate info. The most relevant info is the open Price, outstanding shares, NAV, total asse…

How to create DataFrame with columns based on scraped data?

import requests, re from bs4 import BeautifulSoupdata = []soup = BeautifulSoup(requests.get(https://www.booking.com/searchresults.html?label=gen173nr-1FCAEoggI46AdIM1gEaGyIAQGYATG4ARfIAQzYAQHoAQH4AQKI…

How do i change the colour of a button border tkinter

How do i change the colour of a border in tkinterI have looked at other solutions which recommended using highlightcolor and highlightbackground, however these did not work. excercises_button = Button(…

module object has no attribute Gridspec despite calling help(gridspec) revealing the Gridspec class

If I run the python console and doimport matplotlib matplotlib.__version__ import matplotlib.gridspec as gsI see that the matplotlib version is 1.2.1.If I do help(gs) I see the Gridspec class.However t…