Home > Software design >  How to read a csv file with commas in field with pandas python?
How to read a csv file with commas in field with pandas python?

Time:05-14

Hi I have a csv file with items like this

product_id,url
100,https://url/p/Cimory-Yogurt-Squeeze-Original-120-g-745133
"1000,""https://url/p/OREO-Biskuit-Dark-&-White-Chocolate-123,5-g-559227"""
1002,https:/url/p/GARNIER-Micellar-Cleansing-Water-Sensitive-Skin-Pink-125-ml-371378

I tried using

import pandas as pd
productUrl = pd.read_csv('productUrl.csv',sep=","quotechar='"')

It returns back as

product_id url
100 https://url/p/Cimory-Yogurt-Squeeze-Original-120-g-745133
1000,"https://url/p/OREO-Biskuit-Dark-&-White-Chocolate-123,5-g-559227"
1002 https:/url/p/GARNIER-Micellar-Cleansing-Water-Sensitive-Skin-Pink-125-ml-371378

How do I read the csv? Because the url has commas in there too.

CodePudding user response:

You do not need the quotechar='"', simply read it as is:

pd.read_csv('productUrl.csv')

Be aware that your pandas.read_csv() example wont work cause it is missing a , between parameters

Example
import pandas as pd
from io import StringIO

csvString = """product_id,url
100,https://url/p/Cimory-Yogurt-Squeeze-Original-120-g-745133
1000,"https://url/p/OREO-Biskuit-Dark-&-White-Chocolate-123,5-g-559227"
1002,https:/url/p/GARNIER-Micellar-Cleansing-Water-Sensitive-Skin-Pink-125-ml-371378"""

pd.read_csv(StringIO(csvString))
Output
product_id url
0 100 https://url/p/Cimory-Yogurt-Squeeze-Original-120-g-745133
1 1000 https://url/p/OREO-Biskuit-Dark-&-White-Chocolate-123,5-g-559227
2 1002 https:/url/p/GARNIER-Micellar-Cleansing-Water-Sensitive-Skin-Pink-125-ml-371378
  • Related