Home > Net >  Cant replace spaces in a python variable
Cant replace spaces in a python variable

Time:08-15

i tried to replace spaces in a variable in python but it returns me this error

AttributeError: 'HTTPHeaders' object has no attribute 'replace'

this is my code

for req in driver.requests:
    print(req.headers)
    d = req.headers    


x = d.replace("""
""", "")

CodePudding user response:

So, if you check out the class HTTPHeaders you'll see it has a __repr__ function and that it's an HTTPMessage object.

Depending on what you exactly want to achieve (which is still not clear to me!, i.e, for which header do you want to replace spaces?) you can go about this two ways. Use the methods on the HTTPMessage object (documented here) or use the string version of it by calling repr on the response. I recommend you use the first approach as it is much cleaner.

I'll give an example in which I remove spaces for all canary values in all of the requests:

for req in driver.requests:
    canary = req.headers.get("canary")
    canary = canary.replace(" ", "")

P.S., your question is nowhere near clear enough as it stands. Only after asking multiple times and linking your other question it becomes clear that you are using seleniumwire, for example. Ideally, the code you provide can be run by anyone with the installed packages and reproduces the issue you have. BUT, allright, the comments made it more clear.

  • Related