Escaped quotes in pandas read_csv

前端 未结 1 1850
北荒
北荒 2021-02-13 09:32

I am unable to create a dataframe which has escaped quotes when using read_csv.
(Note: R\'s read.csv works as expected.)

My code:

imp         


        
相关标签:
1条回答
  • 2021-02-13 09:59

    It does work, but you have to indicate the escape character for the embedded quotes:

    In [1]: data = '''SEARCH_TERM,ACTUAL_URL
    "bra tv bord","http://www.ikea.com/se/sv/catalog/categories/departments/living_room/10475/?se%7cps%7cnonbranded%7cvardagsrum%7cgoogle%7ctv_bord"
    "tv p\xc3\xa5 hjul","http://www.ikea.com/se/sv/catalog/categories/departments/living_room/10475/?se%7cps%7cnonbranded%7cvardagsrum%7cgoogle%7ctv_bord"
    "SLAGBORD, \\"Bergslagen\\", IKEA:s 1700-tals serie","http://www.ikea.com/se/sv/catalog/categories/departments/living_room/10475/?se%7cps%7cnonbranded%7cvardagsrum%7cgoogle%7ctv_bord"'''
    
    In [2]: df = read_csv(StringIO(data), escapechar='\\', encoding='utf-8')
    
    In [3]: df
    Out[3]: 
                                          SEARCH_TERM                                         ACTUAL_URL
    0                                     bra tv bord  http://www.ikea.com/se/sv/catalog/categories/d...
    1                                      tv på hjul  http://www.ikea.com/se/sv/catalog/categories/d...
    2  SLAGBORD, "Bergslagen", IKEA:s 1700-tals serie  http://www.ikea.com/se/sv/catalog/categories/d...
    

    see this gist.

    0 讨论(0)
提交回复
热议问题