【问题标题】:Replace all text between < ref > and < /ref > in python替换python中<ref>和</ref>之间的所有文本
【发布时间】:2020-05-23 02:22:25
【问题描述】:

我从 Wikipedia 抓取数据并创建了一个数据框。 df[0] 包含

\n \n == Sifat-sifat DNA == \n  DNA merupakan sebuah polimer yang terdiri dari satuan-satuan berulang yang disebut nukleotida.     Tiap-tiap nukleotida terdiri dari tiga komponen utama, yakni gugus fungsionalgugus fosfat, gula deoksiribosa, dan basa nitrogen (nukleobasa) < ref > {{en}}{{cite web \n  url          = http://www.ncbi.nlm.nih.gov/bookshelf/br.fcgi?book=mboc4 & part=A2 \n  title        = All Cells Replicate Their Hereditary Information by Templated Polymerization \n  accessdate   = 2010-03-19 \n  work         = Bruce Alberts, et al. \n }} < /ref > . Pada DNA, nukleobasa yang ditemukan adalah Adenina (A), Guanina (G), Sitosina (C) dan Timina (T).

我要删除:

< ref > {{en}}{{cite web \n  url          = http://www.ncbi.nlm.nih.gov/bookshelf/br.fcgi?book=mboc4 & part=A2 \n  title        = All Cells Replicate Their Hereditary Information by Templated Polymerization \n  accessdate   = 2010-03-19 \n  work         = Bruce Alberts, et al. \n }} < /ref > 

我需要一种方法来在“”和“ ”之间进行替换(或删除)和文本,这样当我调用它时,df[0] 现在等于:

\n \n == Sifat-sifat DNA == \n  DNA merupakan sebuah polimer yang terdiri dari satuan-satuan berulang yang disebut nukleotida.     Tiap-tiap nukleotida terdiri dari tiga komponen utama, yakni gugus fungsionalgugus fosfat, gula deoksiribosa, dan basa nitrogen (nukleobasa). Pada DNA, nukleobasa yang ditemukan adalah Adenina (A), Guanina (G), Sitosina (C) dan Timina (T).

我试过了:

df['Body'] = df['Body'].str.replace('< ref >.*?< /ref >','',regex=True)
df['Body'] = df['Body'].str.replace('< ref >.*< \/ref >','',regex=True)

但输出仍然没有改变,像这样

\n \n == Sifat-sifat DNA == \n  DNA merupakan sebuah polimer yang terdiri dari satuan-satuan berulang yang disebut nukleotida.     Tiap-tiap nukleotida terdiri dari tiga komponen utama, yakni gugus fungsionalgugus fosfat, gula deoksiribosa, dan basa nitrogen (nukleobasa) < ref > {{en}}{{cite web \n  url          = http://www.ncbi.nlm.nih.gov/bookshelf/br.fcgi?book=mboc4 & part=A2 \n  title        = All Cells Replicate Their Hereditary Information by Templated Polymerization \n  accessdate   = 2010-03-19 \n  work         = Bruce Alberts, et al. \n }} < /ref > . Pada DNA, nukleobasa yang ditemukan adalah Adenina (A), Guanina (G), Sitosina (C) dan Timina (T).

而我需要的就像我之前解释的那样。我找不到任何似乎有效的通配符。非常感谢任何帮助。

【问题讨论】:

  • @PacketLoss 我也试过了,结果和我上面解释的一样
  • 我建议使用regex101.com 来测试你的正则表达式,看看它是如何工作的。
  • 您的正则表达式似乎是正确的:regex101.com/r/X5ydjA/1。也许您没有正确使用replace() 函数?
  • @Code-Apprentice 这个代码:df['Body'] = df['Body'].str.replace('&lt; ref &gt;.*&lt; \/ref &gt;','',regex=True) 不正确吗?
  • 为什么数据在 DataFrame 中?你是怎么刮的?为什么您似乎使用正则表达式来解析 HTML?我认为我们遗漏了一些重要信息。维基百科也没有 API 吗?

标签: python regex


【解决方案1】:

问题在于 Python 正则表达式默认情况下不会将换行符与点匹配。我们能做的就是匹配一切直到结束ref

df['Body'] = df['Body'].str.replace('< ref >[\s\S]*< /ref >', '', regex=True)

我从这里得到了正则表达式的想法:matching any character including newlines in a Python regex subexpression, not globally

【讨论】:

    猜你喜欢
    • 2023-03-18
    • 1970-01-01
    • 2020-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-19
    • 1970-01-01
    相关资源
    最近更新 更多