繁体   English   中英

如何删除 python 中的空格?

[英]How do i remove Blank spaces in python?

from selenium import webdriver
import os, time
from selenium.common.exceptions import NoSuchElementException

PATH = ('C:\\Program Files (x86)\\Chrome\\Application\\chromedriver.exe') 
driver = webdriver.Chrome(PATH)
var2 = input('Enter a Link: ')

while True:
    driver.get(var2)
    time.sleep(3)
    driver.refresh()
    time.sleep(3)
    var = driver.find_elements_by_id('practice')
    print(var[0].text)
    try:
        driver.find_element_by_xpath('/html/body/div[9]/section/div[1]/div[1]/div[6]/div/div[1]/div/div[2]/button').click()
    except NoSuchElementException:
        try:
            driver.find_element_by_xpath('/html/body/div[9]/section/div[1]/div[1]/div[6]/div/div[1]/div/div[2]/button').click()
        except NoSuchElementException:
            try:
                driver.find_element_by_xpath('/html/body/div[11]/section/div[1]/div[1]/div[6]/div/div[1]/div/div[2]/button').click()
            except NoSuchElementException:
                print('error go back to main.')

driver.find_element_by_xpath('/html/body/div[1]/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div/button[2]').click()
time.sleep(1)
myString = driver.find_elements_by_id('bd')[0].text
# clean answer
newString = myString.replace('Got it','')
newStringA = newString.replace('solve','')
newStringB = newStringA.replace('Got it','')
newString1 = newStringB.replace('Questions','')
newString2 = newString1.replace('answered','')
newString3 = newString2.replace('Time','')
newString4 = newString3.replace('elapsed','')
newString5 = newString4.replace('HR MIN SEC','')
newString6 = newString5.replace('SmartScore','')
newString7 = newString6.replace('out of 100','')
newString8 = newString7.replace('SmartScore','')
newString9 = newString8.replace('NEXT PROBLEM','')
newString10 = newString9.replace('Learn with an example','')
newString11 = newString10.replace('You','')
newString12 = newString11.replace('out of','')
newString13 = newString12.replace('So:','Answer:')
newString14 = newString13.replace('review','')
newString15 = newString14.replace('00 00 04','')
newString16 = newString15.replace('The correct answer is:','')
newString17 = newString16.replace('Sorry, incorrect...','')
newString18 = newString17.replace('So:','Answer:')
newString19 = newString18.replace('Submit:','')
newString20 = newString19.replace(':','')


print(newString20)
print('NEXT PROBLEM')
driver.refresh()
driver.delete_all_cookies()

当我打印文本时,文本和大量空白之间存在巨大差距,是否有一种有效且简单的删除它们的方法? 例如, 这里有很多空行,我想删除它们! 这里有很多空白行,我想删除它们吗?

您可以随时尝试:

your_string.replace(" ","")

这将删除所有空格,包括前导和尾随空格

您可以尝试删除每个字符串中的,'' 部分,因为在字符串中添加 '' 只会在 output 中添加一个空格。

为什么不使用条带?

#...The most simple example 

txt = "     hamburgers    "

x = txt.strip()

print("I like", x, ".They are my favorites")

output 将是整个字符串本身,但没有空格。 strip 消除了字符串开头和结尾的所有指定空格。 您也可以将其与其他字符串一起使用

假设 txt 是:

txt = ']hamburger]'
x= txt.strip(']')

如果你打印 x 你会得到 'hamburger'

暂无
暂无

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

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