【问题标题】:Python removing lines that contain more then one tildePython删除包含多个波浪号的行
【发布时间】:2016-03-24 02:03:22
【问题描述】:

我很乐意解决这个问题!我有一个庞大的文本文件(大约 300 万行),我需要删除任何包含一个以上波浪号的行。有人可以帮助我吗!请和谢谢。

这是我目前所拥有的:

import re 

f_in = 'C:\\Users\\John\\Documents\\Python\\Pagelinks\\pagelinkSample_10K_cleaned2.txt'

f_out = 'C:\\Users\\John\\Documents\\Python\\Pagelinks\\pagelinkSample_10K_cleaned3.txt'

with open(f_in, 'r') as fin: 

        with open(f_out, 'w') as fout: 

              for line in fin: 

卡在这里,我在想波浪号的行数是否包含多于一个删除行?

谢谢!

【问题讨论】:

  • grep -v '~~' 工作时为什么要使用 python?

标签: python tilde


【解决方案1】:
for line in fin:
    if line.count('~') < 2:
        fout.write(line)

【讨论】:

  • 建议:if line.count('~') &lt; 2:,去掉continue
猜你喜欢
  • 2015-08-08
  • 1970-01-01
  • 2012-03-08
  • 1970-01-01
  • 1970-01-01
  • 2013-07-25
  • 2019-10-03
相关资源
最近更新 更多