How to read a CSV line with "?

前端 未结 4 1435
刺人心
刺人心 2020-12-18 03:19

A trivial CSV line could be spitted using string split function. But some lines could have \", e.g.

\"good,morning\", 100, 300, \"1998,5,3\"
         


        
4条回答
  •  生来不讨喜
    2020-12-18 04:02

    From python's CSV module:

    reading a normal CSV file:

    import csv
    reader = csv.reader(open("some.csv", "rb"))
    for row in reader:
        print row
    

    Reading a file with an alternate format:

    import csv
    reader = csv.reader(open("passwd", "rb"), delimiter=':', quoting=csv.QUOTE_NONE)
    for row in reader:
        print row
    

    There are some nice usage examples in LinuxJournal.com.

    If you're interested with the details, read "split string at commas respecting quotes when string not in csv format" showing some nice regexen related to this problem, or simply read the csv module source.

提交回复
热议问题