【问题标题】:Iterate and replace words in lines of a tuple python迭代和替换元组python中的单词
【发布时间】:2016-08-03 02:30:33
【问题描述】:

我想遍历这个元组,对于每一行,遍历单词以使用正则表达式查找和替换一些单词(确切地说是互联网地址),同时将它们保留为行。

aList=
[
  "being broken changes people, \nand rn im missing the old me", 
  "@SaifAlmazroui @troyboy621 @petr_hruby you're all missing the point", 
  "#News #Detroit Detroit water customer receives shutoff threat over missing 10 cents: - Theresa Braxton is a l... T.CO/CHPBRVH9WKk", 
  "@_EdenRodwell \ud83d\ude29\ud83d\ude29ahh I love you!! Missing u, McDonald's car park goss soon please \u2764\ufe0f\u2764\ufe0fxxxxx", 
  "This was my ring tone, before I decided change was good and missing a call was insignificant T.CO?BUXLVZFDWQ", 
  "want to go on holiday again, missing the sun\ud83d\ude29\u2600\ufe0f"
]

下面的代码几乎可以做到这一点,但它将列表分成由行分隔的单词:

i=0
while i<len(aList):
    for line in aList[i].split():
        line = re.sub(r"^[http](.*)\/(.*)$", "", line)
        print (line)
        i+=1

我希望得到除每行中的互联网地址之外的结果:

[
  "being broken changes people, \nand rn im missing the old me", 
  "@SaifAlmazroui @troyboy621 @petr_hruby you're all missing the point", 
  "#News #Detroit Detroit water customer receives shutoff threat over missing 10 cents: - Theresa Braxton is a ", 
  "@_EdenRodwell \ud83d\ude29\ud83d\ude29ahh I love you!! Missing u, McDonald's car park goss soon please \u2764\ufe0f\u2764\ufe0fxxxxx", 
  "This was my ring tone, before I decided change was good and missing a call was insignificant", 
  "want to go on holiday again, missing the sun\ud83d\ude29\u2600\ufe0f"
]

谢谢

【问题讨论】:

  • 你有一个无限循环。 Python 不能用 plus plus,你必须做 plus equals。
  • 这是 i++、bpachev 的拼写错误,我已经更正了。 John1024,代码运行时没有出现拼写错误,我不允许在问题中输入互联网地址。地址的例子是(T.CO?BUXLVZFDWQ);我已经把它们全部大写了。
  • regex [http] 表示 1 个字符,即 h、t 或 p。 regex http 表示 4 个字符,依次为 h、t、t 和 p。
  • 是的,Aprillion。我想通了。谢谢

标签: python regex loops replace tuples


【解决方案1】:

从这里:

re.sub(r"^[http](.*)\/(.*)$", "", line)

在我看来,您似乎希望您的所有 URL 都位于行尾。在这种情况下,请尝试:

[re.sub('http://.*', '', s) for s in aList]

在这里,http:// 匹配以http:// 开头的任何内容。 .* 匹配后面的所有内容。

示例

这是您添加了一些 URL 的列表:

aList = [
  "being broken changes people, \nand rn im missing the old me",
  "@SaifAlmazroui @troyboy621 @petr_hruby you're all missing the point",
  "#News #Detroit Detroit water customer receives shutoff threat over missing 10 cents: - Theresa Braxton is a http://example.com/CHPBRVH9WKk",
  "@_EdenRodwell ahh I love you!! Missing u, McDonald's car park goss soon please xxxxx",
  "This was my ring tone, before I decided change was good and missing a call was insignificant http://example.com?BUXLVZFDWQ",
  "want to go on holiday again, missing the sun"
  ]

结果如下:

>>> [re.sub('http://.*', '', s) for s in aList]
['being broken changes people, \nand rn im missing the old me',
 "@SaifAlmazroui @troyboy621 @petr_hruby you're all missing the point",
 '#News #Detroit Detroit water customer receives shutoff threat over missing 10 cents: - Theresa Braxton is a ',
 "@_EdenRodwell ahh I love you!! Missing u, McDonald's car park goss soon please xxxxx",
 'This was my ring tone, before I decided change was good and missing a call was insignificant ',
 'want to go on holiday again, missing the sun']

【讨论】:

    【解决方案2】:

    你的问题有点不清楚,但我想我明白你想要什么

    newlist = [re.sub(r"{regex}", "", line) for line in alist]
    

    应遍历字符串列表并使用 python 列表理解将任何与您的正则表达式模式匹配的字符串替换为空字符串

    旁注:

    仔细查看您的正则表达式,它看起来不像您认为的那样做 我会看一下这个关于在正则表达式中匹配 url 的 stack over flow 帖子

    Regex to find urls in string in Python

    【讨论】:

    • 这可行,但仅遍历行,而不是遍历行中的每个单词。所以它只是替换了一整行只是一个互联网地址。
    猜你喜欢
    • 2010-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多