簡體   English   中英

在Python中編輯多行字符串

[英]Editing Multiline Strings in Python

這里是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硒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'

您可以使用類似這樣的東西。

結果顯示了什么? 也許你忘記了什么。

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

嘗試這個:

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

您需要執行以下操作來解析您的輸出。

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

我希望這會給您提示。 問候。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM