我有一个类型的字符串:
"04/15/2013","A lot of text: numbers 123,412","3,123.99","-","1,123,123,99"
解析它的正则表达式是什么,以便我得到每个逗号分隔的字符串(由“分隔”的字符串)以进一步解析?
我对使用python的正则表达式模块很感兴趣,但如果给出更好的解决方案,那就没关系了
答案 0 :(得分:10)
您可以使用csv
模块:
import csv
text = '"04/15/2013","A lot of text: numbers 123,412","3,123.99","-","1,123,123,99"'
result = next(csv.reader([text]))
# ['04/15/2013', 'A lot of text: numbers 123,412', '3,123.99', '-', '1,123,123,99']
这利用了读者按预期处理引号和字段分隔符的事实。
答案 1 :(得分:1)
将其分开:
>>> s = '"04/15/2013","A lot of text: numbers 123,412","3,123.99","-","1,123,123,99"'
>>> s[1:-1].split('","')
['04/15/2013', 'A lot of text: numbers 123,412', '3,123.99', '-', '1,123,123,99']
你的论证令牌可以包含逗号并不重要,因为没有令牌可以包含","
。
答案 2 :(得分:-1)
其他答案都不正确,使用正则表达式。使用正则表达式
的方式import re
arrayOfStrings = re.split(r',', stringToBeSplit)