python - 在 Python 中编辑多行字符串

标签 python string python-3.x selenium replace

这里是 Python 的新手。我正在运行 selenium 网络驱动程序以从网站查询一些信息(只能从我的组织访问,是的,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

感谢您的帮助。 :)

最佳答案

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'

你可以使用这样的东西。

关于python - 在 Python 中编辑多行字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52239692/

相关文章:

PHP 在插入数据库之前删除空行

python - 如何将 numpy.array2string 形成的字符串转换回数组?

ruby - 如何使用 gsub 使用正则表达式搜索和替换?

将一个 char** 复制到另一个 char** (C)

python - Pandas 时间戳索引到日期字符串列表

python-3.x - CherryPy上传文件

Python 和 PHP SOAP 服务器

python - 检测悬停在 PyQt4 中的特定小部件上

python - 无法通过 Pandas 中的 lambda 填充多列中的 NaN 值

python - 将函数参数分配给 `self`