简体   繁体   中英

Python3 Problem - Last value printing to new line using format / selenium webdriver

My goal: using self.cookie = '{}{}user-data-dir={}'r'{}{}{}'.format("(","'","'",'r',self.line," )") I am setting that to be used in my main script to set a value, ok so my problem? Well I run it, and it is 99.5% the way it needs to be, I can not for hours now figure out why it keeps printing the last ) on a new line, which i am printing it to debug.. it needs to be on the same line so it can be used in actual code like so: ('user-data-dir='r'C:\Users\0\AppData\Local\Google\Chrome\User Data')


    def __init__(self):
        


        with open('cookies.txt', 'r+', encoding='UTF-8') as f:
            for self.line in f:
                
                self.chrome_options = webdriver.ChromeOptions()

                self.cookie = '{}{}user-data-dir={}'r'{}{}{}'.format("(","'","'",'r',self.line," )")
                print(self.cookie)
CookieSet()

rstrip() is what you want to remove the newline:

            self.cookie = '{}{}user-data-dir={}'r'{}{}{}'.format("(","'","'",'r',self.line.rstrip()," )")

Use string f string is much cleanerz also can use just trim to remove line from both left and right of the string.

self.cookie= (f"(user-data-dir='{self.line.strip()}')")

print(self.cookie)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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