简体   繁体   English

在Python中编辑多行字符串

[英]Editing Multiline Strings in Python

Pretty new to Python here. 这里是Python的新手。 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). 我正在运行Selenium Web驱动程序以便从网站查询某些信息(只能从我的组织访问,是的,SQL查询会更好,但这是我目前正在努力的工作)。 I am using Selenium's .text method to retrieve text from a table and I print(XXX.text) , this returns something like this. 我正在使用Selenium的.text方法从表中检索文本,并且我执行print(XXX.text) ,这返回了类似的内容。

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: 问题是我想删除[Remove]以便剩下类似以下内容的东西:

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 Python 3.5硒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. 问候。

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

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