CU
分类: Python/Ruby
2016-09-12 14:45:14
原文地址:python 以逗号分割,忽略引号内的逗号 作者:xyaxlz
python 以逗号分割,忽略引号内的逗号
加posix=True 和不加posix=True 有区别
前两个是加和不加posix=True的对比,最后一个例子是以空格分割语句的例子lex.quotes = '"' 去掉效果一样
import shlex
str=shlex.shlex("ab,'cdsfd,sfsd',ewewq,5654",posix=True)
str.whitespace=','
str.whitesapce_split=True
b=list(str)
print b
['ab', 'cdsfd,sfsd', 'ewewq', '5654']
import shlex
str=shlex.shlex("ab,'cdsfd,sfsd',ewewq,5654")
str.whitespace=','
str.whitesapce_split=True
b=list(str)
print b
['ab', "'cdsfd,sfsd'", 'ewewq', '5654']
import shlex
lex = shlex.shlex('''This string has "some double quotes" and 'some single quotes'.''')
lex.quotes = '"'
lex.whitespace_split = True
b=list(lex)
['This', 'string', 'has', '"some double quotes"', 'and', "'some", 'single', "quotes'."]