如何在 pandas python 字段中读取带逗号的 csv 文件?
How to read a csv file with commas in field with pandas python?
您好,我有一个包含类似项目的 csv 文件
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
我试过使用
import pandas as pd
productUrl = pd.read_csv('productUrl.csv',sep=","quotechar='"')
它returns返回
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
如何读取 csv?因为 url 里面也有逗号。
您不需要 quotechar='"'
,只需按原样阅读即可:
pd.read_csv('productUrl.csv')
请注意,您的 pandas.read_csv()
示例将无法运行,因为它缺少参数之间的 ,
例子
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))
输出
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
您好,我有一个包含类似项目的 csv 文件
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
我试过使用
import pandas as pd
productUrl = pd.read_csv('productUrl.csv',sep=","quotechar='"')
它returns返回
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 |
如何读取 csv?因为 url 里面也有逗号。
您不需要 quotechar='"'
,只需按原样阅读即可:
pd.read_csv('productUrl.csv')
请注意,您的 pandas.read_csv()
示例将无法运行,因为它缺少参数之间的 ,
例子
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))
输出
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 |