Q:

split a text by ingoring values with quotation

I am new to python and trying to split a text in a specific way,ignoring the commas in sub string when inside ""
text='ppr5007801780,https://www.jcpenney.com/p/alfred-dunner-womens-3-4-sleeve-tunic-top/ppr5007801780,JCPenney,58.0,28.99,"https://s7d4.scene7.com/is/image/JCPenney/DP0208201907032983M.tif?wid=350&hei=350&op_usm=.4,.8,0,0&resmode=sharp2",,81730320182,Alfred Dunner Womens 3/4 Sleeve Tunic Top,Closure Type:Pullover Head|Neckline:Collar Neck|Sleeve Length:3/4 Sleeve|Apparel Length:24.5 Inches,alfred dunner,3,5.0,Navy White,"Embroidered, Scalloped",,/d/women,Available,1572644741'

I have the above string which I would like to use the split(',') ignoring the the commas inside quotations (i.e get len(my_list)=19)
I tried using my_list=text.split(',') but I get 23, I am not sure on how to do it using regex or some other method.
any help is appreciated 

A:

You can use the csv module. In order to pass the csv.reader a string, you need to turn it into a file-like object, which can be achieved using StringIO.
import csv
from io import StringIO
text='ppr5007801780,https://www.jcpenney.com/p/alfred-dunner-womens-3-4-sleeve-tunic-top/ppr5007801780,JCPenney,58.0,28.99,"https://s7d4.scene7.com/is/image/JCPenney/DP0208201907032983M.tif?wid=350&hei=350&op_usm=.4,.8,0,0&resmode=sharp2",,81730320182,Alfred Dunner Womens 3/4 Sleeve Tunic Top,Closure Type:Pullover Head|Neckline:Collar Neck|Sleeve Length:3/4 Sleeve|Apparel Length:24.5 Inches,alfred dunner,3,5.0,Navy White,"Embroidered, Scalloped",,/d/women,Available,1572644741'

f = StringIO(text)

list = csv.reader(f, delimiter=',', quotechar = '"')
for r in list:
    print(len(r))

The csv reader allows you to specify a "quotechar" parameter, which I believe essentially solves your problem.

