简体   繁体   English

Python3 问题 - 使用格式 / selenium webdriver 将最后一个值打印到新行

[英]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?我的目标:使用 self.cookie = '{}{}user-data-dir={}'r'{}{}{}'.format("(","'","'",'r', self.line," )") 我正在设置它以在我的主脚本中使用来设置一个值,好吧我的问题? 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')好吧,我运行它,它是它需要的 99.5%,我现在无法弄清楚为什么它一直打印最后一个)在一个新行上,我正在打印它来调试.. 它需要在同一行,因此可以在实际代码中使用,如下所示: ('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: rstrip() 是您要删除换行符的内容:

            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.使用 string f string is much cleanz 也可以使用 just trim 从字符串的左侧和右侧删除线。

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

print(self.cookie)

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

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