ai didi

python - 在 python 中用不在引号内的特定逗号分割

转载 作者:行者123 更新时间:2023-12-01 19:21:44 24 4
gpt4 key购买 nike

我有这个字符串,我想用“,”分割它

x = 'a, b, c , d , "x,x,2" , hi'
x.split(',')

这是我真正的字符串

x = 'Outward   ,Supply , ,Tax Invoice ,IN9195212470,31/12/2019,VPS AGRO & AUTO PVT LTD ,311954,06AAACV9344F1ZA ,"VILLAGE KHANPUR KOLIAN, N.H. 1 ",6 K.M. FRO,KURUKSHETRA   ,HARYANA ,136131,VPS AGRO & AUTO PVT LTD ,311954,"VILLAGE KHANPUR KOLIAN, N.H. 1",6 K.M. FRO,KURUKSHETRA                             ,HARYANA             ,136131,503675,SM VAL. GENUINE DIESEL ENG. OIL 1/9 L   ,27101980,360,LTR,58204.04,9,5238.36,9,5238.36,0,0,0,0,0,0,0,68680.76,                    ,                    ,                              ,          ,          ,,               ,          ,06AAACW0287A1ZR   ,VALVOLINE CUMMINS PVT LTD-AMBALA        ,"KHASHRA NO-108/1/2,                                         ",          ,AMBALA                                  ,133004,HARYANA             ,                    ,                  ,                              , ,'

它返回这个结果

['a','b','c','d','"x','x','2', 'hi']

但是我想要这个

['a', 'b', 'c' , 'd' , '"x,x,2"' , 'hi']

如何在 python 中做到这一点

帮帮我

最佳答案

import shlex
lexer = shlex.shlex('a, b, c , d , "x,x,2" , hi')
lexer.whitespace += ','
print(list(lexer))

结果:

['a', 'b', 'c', 'd', '"x,x,2"', 'hi']

这是更新任务的更新解决方案:


x = 'Outward ,Supply , ,Tax Invoice ,IN9195212470,31/12/2019,VPS AGRO & AUTO PVT LTD ,311954,06AAACV9344F1ZA ,"VILLAGE KHANPUR KOLIAN, N.H. 1 ",6 K.M. FRO,KURUKSHETRA ,HARYANA ,136131,VPS AGRO & AUTO PVT LTD ,311954,"VILLAGE KHANPUR KOLIAN, N.H. 1",6 K.M. FRO,KURUKSHETRA ,HARYANA ,136131,503675,SM VAL. GENUINE DIESEL ENG. OIL 1/9 L ,27101980,360,LTR,58204.04,9,5238.36,9,5238.36,0,0,0,0,0,0,0,68680.76, , , , , ,, , ,06AAACW0287A1ZR ,VALVOLINE CUMMINS PVT LTD-AMBALA ,"KHASHRA NO-108/1/2, ", ,AMBALA ,133004,HARYANA , , , , ,'


import shlex
lexer = shlex.shlex(x)
lexer.whitespace = ','
lexer.whitespace_split = True
print([cell.strip() for cell in lexer])

结果:

['Outward', 'Supply', '', 'Tax Invoice', 'IN9195212470', '31/12/2019', 'VPS AGRO & AUTO PVT LTD', '311954', '06AAACV9344F1ZA', '"VILLAGE KHANPUR KOLIAN, N.H. 1 "', '6 K.M. FRO', 'KURUKSHETRA', 'HARYANA', '136131', 'VPS AGRO & AUTO PVT LTD', '311954', '"VILLAGE KHANPUR KOLIAN, N.H. 1"', '6 K.M. FRO', 'KURUKSHETRA', 'HARYANA', '136131', '503675', 'SM VAL. GENUINE DIESEL ENG. OIL 1/9 L', '27101980', '360', 'LTR', '58204.04', '9', '5238.36', '9', '5238.36', '0', '0', '0', '0', '0', '0', '0', '68680.76', '', '', '', '', '', '', '', '06AAACW0287A1ZR', 'VALVOLINE CUMMINS PVT LTD-AMBALA', '"KHASHRA NO-108/1/2,                                         "', '', 'AMBALA', '133004', 'HARYANA', '', '', '', '']

关于python - 在 python 中用不在引号内的特定逗号分割,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59800099/

24 4 0
文章推荐: java - Android 上的装箱和拆箱
文章推荐: ios - Mac和iOS Safari src播放html5视频的路径要求?
文章推荐: objective-c - iOS:TapGestureRecognizer 问题
文章推荐: java - JNDI LDAP 池连接
行者123
个人简介

我是一名优秀的程序员,十分优秀!

滴滴打车优惠券免费领取
滴滴打车优惠券
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com