Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中编辑多行字符串_Python_String_Python 3.x_Selenium_Replace - Fatal编程技术网

在Python中编辑多行字符串

在Python中编辑多行字符串,python,string,python-3.x,selenium,replace,Python,String,Python 3.x,Selenium,Replace,这对Python来说是个新概念。我正在运行SeleniumWeb驱动程序,以便从网站查询一些信息(只能从我的组织访问,是的,SQL查询会更好,但这是我目前正在使用的)。我使用Selenium的.text方法从表中检索文本,然后我打印(XXX.text),这将返回如下内容 XXX.pdf [Remove] XXX.pdf [Remove] etc... 问题是我想删除,[remove],这样我就剩下了如下内容: XXX.pdf XXX.pdf 甚至更好 XXX.pdf, XXX.pdf 这是

这对Python来说是个新概念。我正在运行SeleniumWeb驱动程序,以便从网站查询一些信息(只能从我的组织访问,是的,SQL查询会更好,但这是我目前正在使用的)。我使用Selenium的
.text
方法从表中检索文本,然后我
打印(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

谢谢你的帮助。:)

你可以用这样的东西

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

您可以使用类似的东西。

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

XXX.pdf
[Remove]
XXX.pdf
[Remove]
etc...
dataElement=driver。通过css\u选择器(“blah-blah-blah”)查找元素
datasheets=str(dataElement.text) datasheets=datasheets.replace(“[Remove]”,“)
打印(数据表)

打印结果是什么?也许你忘了什么

dataElement=driver。通过css\u选择器(“blah-blah-blah”)查找元素
datasheets=str(dataElement.text) datasheets=datasheets.replace(“[Remove]”,“) 打印(数据表)

尝试以下方法:

l = s.split('[Remove]')
s = ', '.join(l)
试试这个:

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

我希望这能给你一个提示。问候。

您需要执行类似的操作来解析输出

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

我希望这能给你一个提示。问候。

在我尝试删除不需要的字符串之前和之后,它都打印了我的第一个代码段portions@JohnKulick这是因为.replace方法不会更改字符串,它只返回包含替换部分的新字符串,但不会将其分配给某个变量。您需要像这样编写:
dataElement=driver.find_element_by_css_selector(''blah blah blah'')
datasheets=str(dataElement.text)
datasheets=datasheets.replace('[Remove]','')
打印(datasheets)
它打印了我的第一个代码片段,在尝试删除不需要的字符串之前和之后portions@JohnKulick这是因为.replace方法不会更改字符串,它只返回包含替换部分的新字符串,但不会将其分配给某个变量。您需要像这样编写:
dataElement=driver.find_element\u by_css_selector(''blah blah blah'')
datasheets=str(dataElement.text)
datasheets=datasheets.replace('[Remove]','')
打印(datasheets)
我不确定如何使用这段代码。我试着运行这段代码,它给出了语法错误。我试图删除输入和输出以及括号内的数字,在打印字符串“data”时,我收到了一个与输入完全相同的字符串。我遗漏了什么?我编辑了我的答案。只需将数据和要删除的部分数据传递给名为func.的函数就可以了。如果我能投票支持你的答案,我会的。我不知道如何使用这段代码。我试着运行这段代码,它给出了语法错误。我试图删除输入和输出以及括号内的数字,在打印字符串“data”时,我收到了一个与输入完全相同的字符串。我遗漏了什么?我编辑了我的答案。只需将数据和要删除的部分数据传递给名为func.的函数就可以了,如果我能投票支持你的答案,我会的。