Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to read a csv file with commas in field with pandas python?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading