【问题标题】:Find lines from number of characters从字符数中查找行
【发布时间】:2021-09-18 18:18:14
【问题描述】:

大家好,我在记事本++中有一个包含 71989 行的文件,大多数行有 11 个逗号 (,),因为我有 11 列但是当我在 SQL 中加载我的文件时我得到一些错误,因为有些行有 9 条记录,所以9个逗号。 notepad++ 中有没有办法找到哪些行有 9 个逗号而不是 11 个?

我也可以用python。

提前谢谢你

【问题讨论】:

    标签: find notepad++ line


    【解决方案1】:
    • Ctrl+M
    • 查找内容:^[^,]*(?:,[^,]*){9}$
    • 检查 环绕
    • CHECK 正则表达式
    • 检查Bookmark line
    • 全部查找

    说明:

    ^           # beginning of line
        [^,]*       # 0 or more non comma
        (?:         # start non capture group
            ,           # a comma
            [^,]*       # 0 or more non comma
        ){9}        # end group, must appear 9 times
    $           # end of line
    

    截图:

    【讨论】:

      【解决方案2】:

      使用 Python,您可以计算每行出现的所有逗号...

      '''
      
      contents of data.txt (your SQL file for example)...
      
      1,2,3,4,5,6,7,8,9,10,11,
      ,,,,,,,,,,,
      ,,,,,,,,,
      ,,,,,,,,,,,
      1,2,3,4,5,6,7,8,9,
      
      Note: Nine commas on the third and fifth lines.
      
      '''
      
      with open('data.txt') as f:
          lines = f.readlines()
          
      for line_number, line in enumerate(lines, 1):
          if line.count(',') == 9:
              print('Check line number: ' + str(line_number)) 
      

      输出:

      Check line number: 3
      Check line number: 5
      

      也许这有帮助:o?

      【讨论】:

        猜你喜欢
        • 2011-11-06
        • 1970-01-01
        • 2020-10-13
        • 1970-01-01
        • 2014-01-17
        • 1970-01-01
        • 2021-03-19
        • 1970-01-01
        相关资源
        最近更新 更多