importjava.io.{BufferedReader, File, FileInputStream, InputStreamReader}import org.apache.commons.csv.{CSVFormat, CSVParser, QuoteMode}import scala.collection.JavaConverters._object Test extends App { val file = new File("xxx") val br = new BufferedReader(new InputStreamReader(new FileInputStream(file))) val csvFileFormat = CSVFormat.DEFAULT.withEscape('\\').withQuote('"') val csvParser = new CSVParser(br, csvFileFormat) val records = csvParser.getRecords for (record <- records.asScala) { val sb = new StringBuilder() val length = record.size() (0 until length).foreach(i => { sb.append(record.get(i)) sb.append(",") }) println(sb.toString) }}
import csv
with open(FILE_NAME, "rb") as f:
reader = csv.reader(f, quotechar='"', escapechar='\\')
for line in reader:
print(line)