按逗号分割以及如何从 split 中的引号中排除逗号... Python [英] Split by comma and how to exclude comma from quotes in split ... Python

查看:18
本文介绍了按逗号分割以及如何从 split 中的引号中排除逗号... Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

python 2.7 代码

python 2.7 code

cStr = '"aaaa","bbbb","ccc,ddd"' 

newStr = cStr.split(',')

print newStr 

# result : ['"aaaa"','"bbbb"','"ccc','ddd"' ]

但是,我想要这个结果.

but, I want this result.

result = ['"aaa"','"bbb"','"ccc,ddd"'] 

帮助..

推荐答案

使用re.split() 函数:

import re

cStr = '"aaaa","bbbb","ccc,ddd"'
newStr = re.split(r',(?=")', cStr)

print newStr

输出:

['"aaaa"', '"bbbb"', '"ccc,ddd"']

<小时>

,(?=") - 前瞻肯定断言,确保分隔符 , 后跟双引号 "


,(?=") - lookahead positive assertion, ensures that delimiter , is followed by double quote "

这篇关于按逗号分割以及如何从 split 中的引号中排除逗号... Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆