在Python中编辑多行字符串

问题描述 投票:1回答:4

这里对Python很新。我正在运行selenium web驱动程序,以便从网站查询一些信息(只能从我的组织访问,是的SQL查询会好得多,但这是我目前正在工作的)。我正在使用Selenium的.text方法从表中检索文本而我print(XXX.text),这将返回这样的内容。

XXX.pdf
[Remove]
XXX.pdf
[Remove]
etc...

问题是我想删除,[Remove],以便我留下类似的东西:

XXX.pdf
XXX.pdf

甚至更好

XXX.pdf, XXX.pdf

这是我到目前为止所尝试过的,但没有奏效。

dataElement = driver.find_element_by_css_selector('''blah blah blah''')                                             
datasheets = str(dataElement.text)
datasheets.replace('[Remove]','')
print(datasheets)

Python 3.5 Selenium 2

谢谢你的帮助。 :)

python string python-3.x selenium replace
4个回答
2
投票
In [26]: data = '''\
    ...: XXX.pdf
    ...: [Remove]
    ...: XXX.pdf
    ...: [Remove]\
    ...: '''

In [27]: def func(string, rep):
    ...:     return ', '.join([x for x in string.split('\n') if x != rep])
    ...: 

In [28]: func(data, '[Remove]')
Out[28]: 'XXX.pdf, XXX.pdf'

你可以使用类似的东西。


2
投票

它在结果中打印了什么?也许你忘了什么。

dataElement = driver.find_element_by_css_selector('''blah blah blah''')
datasheets = str(dataElement.text) datasheets = datasheets.replace('[Remove]','') print(datasheets)


0
投票

试试这个:

l = s.split('[Remove]')
s = ', '.join(l)

0
投票

你需要做这样的事情来解析你的输出。

dataElement = driver.find_element_by_css_selector("blah blah blah")
#I don't know what type is this one, but I asume it's a iterable. 

removes = Set(["[remove]","[remove1]", "[remove2]"])
#You can have a set of the strings you want to remove
for data in dataElement:
#for every unit in this iterable variable we'll do the next lines
    if str(data) in removes == False:
    #if something it is not actually in the set of unwanted stuff.                          
        print(str(data))
        #this is your useful output
        #whatever you wanna do to the filtered output.
    else:
        #this is the stuff you don't want to use, the [remove] ones

我希望这会给你一个暗示。问候。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.