【问题标题】:Capture text between ','捕获“,”之间的文本
【发布时间】:2019-04-26 15:23:37
【问题描述】:

我有一行包含逗号的文本。我想捕获逗号之间的数据。

line = "",,,,,,,,,ce: appears to assume ,that\n

我使用的是正则表达式捕获模式 = (""),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*)\\n

输出是:

Output 1
1.  ""
2.  ,
3.  Empty
4.  Empty
5.  Empty
6.  Empty
7.  Empty
8.  Empty
9.  ce: appears to assume
10. that

我想输出为:

Output 2
1.  ""
2.  Empty
3.  Empty
4.  Empty
5.  Empty
6.  Empty
7.  Empty
8.  Empty
9.  Empty
10. ce: appears to assume, that

基本上我正在寻找某种通用的贪婪方法,它忽略文本之间的逗号','

【问题讨论】:

  • 您是否有理由要使用正则表达式?简单地拆分(九个)逗号。 line.split(',', 9)
  • 等等,您希望最后一个逗号的处理方式与其他逗号不同吗?此外,如果您删除最后一个,(.*),您应该得到您想要的输出...
  • 您能否阐明确切的标准? “忽略文本之间的逗号”是什么意思?之间还有什么?
  • @AdamSmith:我想使用正则表达式来获得通用解决方案,因为逗号可以出现在文本中两个逗号之间的任何位置。但是是的,输入结构是固定的。我将检查拆分的行为。
  • @C.Nivs 同意。但也可以提出建议,以检查是否可以在 tabula.py 中将分隔符 , 重新定义为 ;。然后使用 python 的 csv 模块进行评估...

标签: python regex csv


【解决方案1】:

Regex 在这里似乎是错误的解决方案。如果您知道要匹配多少个匹配项(规定为 10 个),那么您就知道您期望使用多少个逗号。使用str.split

>>> line.split(',', 9)
['""', '', '', '', '', '', '', '', '', 'ce: appears to assume ,that\n']

【讨论】:

  • 请注意,如果您可以控制输入,则应该绝对将其更改为更标准的内容。即使“更标准”是引用的 csv。程序不必猜测只有 9 个逗号定义了您的数据结构,而任何额外的逗号都是数据本身的一部分。
【解决方案2】:

您可以在此处使用itertools.groupby 来过滤长度:

import itertools

someline = '"",,,,,,,,ce: appears to assume ,that\n'

# Group by length greater than 0
res = [(i, ','.join(x)) for i,x in itertools.groupby(someline.split(','), key=lambda x: len(x)>0)]

# [(True, '""'), (False, ',,,,,,'), (True, 'ce: appears to assume ,that\n')]

# Then you can just gather your results
results = []
for i, x in res:
    if i is True:
        results.append(x)
    else:
        results.extend(x.split(','))

results
# ['""', '', '', '', '', '', '', '', 'ce: appears to assume ,that\n']

这避免了您必须检查一定数量的逗号,如果这不是每行的固定值。

不同的格式

但是,我认为真正的问题是逗号不仅仅是分隔符,也是数据中的元素,这使得这个问题有点模棱两可。根据docs,您似乎可以指定不同的输出格式,例如.tsv,这将由\t 分隔并完全避免该问题:

tabula.convert_into("test.pdf", "output.tsv", output_format="tsv", pages='all')

那么你的线条看起来像:

someline = '""\t\t\t\t\t\t\t\tce: appears to assume ,that\n'

# Much easier to handle
someline.split('\t')

# ['""', '', '', '', '', '', '', '', 'ce: appears to assume ,that\n']

【讨论】:

    【解决方案3】:

    问题是.* 匹配的字符太多,包括逗号。您应该创建匹配所有字符 except 用于逗号的组,例如

    ^(""),([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),(.*)$
    

    最后一个可以匹配逗号,这样就可以匹配ce: appears to assume ,that中的逗号

    #!/usr/bin/env python
    
    import re
    
    reg = re.compile('^(""),([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),(.*)$')
    
    match = reg.match('"",,,,,,,,,ce: appears to assume ,that\n')
    
    for i in range(1,11):
        print('{:>2s}.  {}'.format(str(i),"Empty" if len(match.group(i))==0 else match.group(i)))
    

    提供所需的输出

     1.  ""
     2.  Empty
     3.  Empty
     4.  Empty
     5.  Empty
     6.  Empty
     7.  Empty
     8.  Empty
     9.  Empty
    10.  ce: appears to assume ,that```
    

    【讨论】:

      【解决方案4】:

      不知道你是否需要所有的空。愿这就是你要找的东西

      separados = line.split(',,')
      
      for i in range(len(separados)):
          try:  #you can add more custom filters here
              if separados[i][0] == ',': separados[i] = separados[i][1:]
          except: pass
          try:
              if separados[i][-1] == ',': separados[i] = separados[i][:-1]
          except: pass
      

      这就是你得到的

      '""'
      ''
      ''
      ''
      'ce: appears to assume ,that\n'
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-04-23
        • 2021-04-03
        • 1970-01-01
        • 2013-04-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-23
        相关资源
        最近更新 更多