如何从一个大字符串中分离出一些字符?

时间:2019-08-02 08:59:21

标签: python-3.x

我创建了一个大字符串,并将其字符放入名为chars的列表中。我想从中分离一些特定的字符。

例如:

chars = ["ü","ç","ö","a","ş","ğ","ı","d"]
chars2 = []

在Windows上只是一个带有pycharm的简单python 3.7

    for i in chars:
        if i != "ü" or "ğ" or "ç" or "ö" or "ş" or "ı":
            chars2.append(i)
    print(chars2)

预期结果:['a','d']

结果:['ü','ç','ö','a','ş','ğ','ı','d']

2 个答案:

答案 0 :(得分:1)

为了获得更好的可维护性,您可以将要过滤的字符放入一个单独的列表中,然后执行以下操作:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import json

driver = webdriver.Chrome()
driver.get('https://www.lanebryant.com/perfect-sleeve-swing-tunic-top/prd-356831')
item = WebDriverWait(driver, 10).until(EC.presence_of_element_located(
    (By.XPATH, "//script[@type='text/javascript'][contains(.,'window.lanebryantDLLite')]"))).get_attribute('innerHTML')
itemtext = item.split("=")[1].split(";")[0]  # This will return as string

itemjson = json.loads(itemtext.strip())  # Converted here into json format

itemtop = itemjson['page']['pageName']  # Use the parameter to get the text

print(itemtop.split(':')[1].strip())  # Split here to get only value tops

答案 1 :(得分:0)

请按照以下说明进行更改,以使您的代码正常工作

    for i in chars:
        if i != "ü" and i!= "ğ" and i!="ç" and i!= "ö" and i!="ş" and i!="ı":
            chars2.append(i)
    print(chars2)