简体   繁体   中英

Editing Multiline Strings in Python

Pretty new to Python here. I am running selenium web driver in order to query some info from a website (only accessible from my organization, yes SQL queries would be much better but this is what I have working at the moment). I am using Selenium's .text method to retrieve text from a table and I print(XXX.text) , this returns something like this.

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

The question is I would like to remove, the [Remove] so that I am left with something like:

XXX.pdf
XXX.pdf

or even better

XXX.pdf, XXX.pdf

This is what I have tried so far which has not worked.

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

Python 3.5 Selenium 2

Thanks for any help. :)

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'

You can use something like this.

What did it print in result? Maybe You forget something.

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

try this:

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

You need to do something like this to parse your output.

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

I hope this gives you a hint. Greetings.

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