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

前端 未结 4 1165
深忆病人
深忆病人 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:58

    The docs and experimentation prove that only single-character delimiters are allowed.

    Since cvs.reader accepts any object that supports iterator protocol, you can use generator syntax to replace ||-s with |-s, and then feed this generator to the reader:

    def read_this_funky_csv(source):
      # be sure to pass a source object that supports
      # iteration (e.g. a file object, or a list of csv text lines)
      return csv.reader((line.replace('||', '|') for line in source), delimiter='|')
    

    This code is pretty effective since it operates on one CSV line at a time, provided your CSV source yields lines that do not exceed your available RAM :)

提交回复
热议问题