How to use python csv module for splitting double pipe delimited data

前端 未结 4 1167
深忆病人
深忆病人 2020-12-19 06:12

I have got data which looks like:

\"1234\"||\"abcd\"||\"a1s1\"

I am trying to read and write using Python\'s csv reader and writer. As the

4条回答
  •  没有蜡笔的小新
    2020-12-19 06:54

    If your data literally looks like the example (the fields never contain '||' and are always quoted), and you can tolerate the quote marks, or are willing to slice them off later, just use .split

    >>> '"1234"||"abcd"||"a1s1"'.split('||')
    ['"1234"', '"abcd"', '"a1s1"']
    >>> list(s[1:-1] for s in '"1234"||"abcd"||"a1s1"'.split('||'))
    ['1234', 'abcd', 'a1s1']
    

    csv is only needed if the delimiter is found within the fields, or to delete optional quotes around fields

提交回复
热议问题