【问题标题】:Python, split a string at commas, except within quotes, ignoring whitespacePython,以逗号分隔字符串,引号内除外,忽略空格
【发布时间】:2019-03-28 11:21:12
【问题描述】:

我找到了一些解决方案,但得到的结果与我的预期不符。

我想获取一个字符串,并以逗号分隔它,除非逗号包含在双引号中。我想忽略空格。我可以忍受在此过程中丢失双引号,但这不是必需的。

csv 是最好的方法吗?正则表达式解决方案会更好吗?

#!/usr/local/bin/python2.7

import csv

s = 'abc,def, ghi, "jkl, mno, pqr","stu"'

result = csv.reader(s, delimiter=',', quotechar='"')

for r in result: 
    print r

# Should display:
# abc
# def
# ghi
# jkl, mno, pqr
# stu
#
# But I get:
# ['a']
# ['b']
# ['c']
# ['', '']
# ['d']
# ['e']
# ['f']
# ['', '']
# [' ']
# ['g']
# ['h']
# ['i']
# ['', '']
# [' ']
# ['jkl, mno, pqr']
# ['', '']
# ['stu']

print r[1]  # Should be "def" but I get and "list index out of range" error.

【问题讨论】:

    标签: python regex csv split


    【解决方案1】:

    你可以使用正则表达式

    ".+?"|[\w-]+
    

    这将匹配双引号,后跟任何字符,直到找到下一个双引号 - 或者,它将匹配单词字符(没有逗号或引号)。

    https://regex101.com/r/IThYf7/1

    import re
    s = 'abc,def, ghi, "jkl, mno, pqr","stu"'
    for r in re.findall(r'".+?"|[\w-]+', s):
        print(r)
    

    如果你想摆脱引用部分周围的"s,我可以通过使用regex 模块(以便\K 可用)找出最好的方法是:

    (?:^"?|, ?"?)\K(?:(?<=").+?(?=")|[\w-]+)
    

    https://regex101.com/r/IThYf7/3

    【讨论】:

    • 工作得几乎完美......但由于某种原因,它也在“-”字符上分裂。我应该在示例字符串中包含一些带连字符的数据,例如 hello-there..
    • 使用字符集而不仅仅是\w,因为\w- 不匹配 - 将最后的替换更改为[\w-]+
    • 这里的点字符的问题是,它将适用于直到字符串结尾的每个字符。所以它应该替换为除双引号之外的所有字符集:[^"]
    • 谢谢!只是使用您提供的链接自己弄清楚了。当我能像英语一样阅读正则表达式时会很好。 :)
    • @SvenKrüger 不,因为重复是 lazy - 它会匹配尽可能少的字符,直到找到下一个 "。 (查看 regex101 链接。)我认为,如果可能的话,延迟重复比否定字符集更好。
    【解决方案2】:

    除了使用csv 之外,您还可以使用另一种不错的方法,该方法由较新的regex 模块(即pip install regex)支持:

    "[^"]*"(*SKIP)(*FAIL)|,\s*
    


    内容如下:
    "[^"]*"(*SKIP)(*FAIL) # match everything between two double quotes and "forget" about them
    |                     # or
    ,\s*                  # match a comma and 0+ whitespaces
    


    Python:
    import regex as re
    
    rx = re.compile(r'"[^"]*"(*SKIP)(*FAIL)|,\s*')
    string = 'abc,def, ghi, "jkl, mno, pqr","stu"'
    
    parts = rx.split(string)
    print(parts)
    

    这会产生

    ['abc', 'def', 'ghi', '"jkl, mno, pqr"', '"stu"']
    

    a demo on regex101.com

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-17
      • 2018-05-20
      • 2011-12-25
      • 2020-04-05
      相关资源
      最近更新 更多