Every language
10 languages, copy-ready. One at a time with syntax highlighting, or all inline.
JSJavaScript
import { parse } from 'csv-parse/sync';
import { readFile } from 'node:fs/promises';
const rows = parse(await readFile('data.csv', 'utf8'), { columns: true });
console.log(rows[0].name);columns: true consumes the header row and yields objects. csv-parse is the de-facto Node CSV package.
TSTypeScript
import { parse } from 'csv-parse/sync';
import { readFile } from 'node:fs/promises';
interface Row {
name: string;
email: string;
}
const rows = parse(await readFile('data.csv', 'utf8'), {
columns: true,
}) as Row[];
console.log(rows[0]?.name);The cast is trust, not a check — validate untrusted files with a schema library before use.
GoGo
package main
import (
"encoding/csv"
"fmt"
"os"
)
func main() {
f, err := os.Open("data.csv")
if err != nil {
panic(err)
}
defer f.Close()
rows, err := csv.NewReader(f).ReadAll()
if err != nil {
panic(err)
}
fmt.Println(rows[1][0]) // first data row, first column
}encoding/csv handles quotes and embedded newlines; FieldsPerRecord enforces consistent column counts.
RsRust
use std::fs::File;
use std::io::BufReader;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let file = File::open("data.csv")?;
let mut reader = csv::Reader::from_reader(BufReader::new(file));
for result in reader.records() {
let record = result?;
println!("{}", &record[0]);
}
Ok(())
}csv = "1" in Cargo.toml — the de-facto crate, quote-aware and streaming.
PHPPHP
<?php
$handle = fopen('data.csv', 'r');
$headers = fgetcsv($handle);
while (($row = fgetcsv($handle)) !== false) {
$record = array_combine($headers, $row);
echo $record['name'], PHP_EOL;
}
fclose($handle);fgetcsv is quote-aware. PHP 8.4+ warns unless you pass the escape argument explicitly — fgetcsv($handle, null, ',', '"', '\\').
PyPython
import csv
with open('data.csv', newline='', encoding='utf-8') as f:
for row in csv.DictReader(f):
print(row['name'])newline='' is required — the csv module manages its own line endings, and without it embedded newlines in quoted fields break.
C#C#
using Microsoft.VisualBasic.FileIO;
using var parser = new TextFieldParser("data.csv")
{
TextFieldType = FieldType.Delimited,
};
parser.SetDelimiters(",");
while (!parser.EndOfData)
{
var fields = parser.ReadFields();
Console.WriteLine(fields[0]);
}TextFieldParser ships with the .NET SDK and is quote-aware — the pragmatic built-in answer. CsvHelper is the community favorite for heavy use.
JvJava
import com.opencsv.CSVReader;
import com.opencsv.exceptions.CsvException;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
public class ReadCsv {
public static void main(String[] args) throws IOException, CsvException {
try (CSVReader reader = new CSVReader(new FileReader("data.csv"))) {
List<String[]> rows = reader.readAll();
System.out.println(rows.get(1)[0]);
}
}
}Dependency com.opencsv:opencsv. readAll() slurps the file — iterate with reader directly to stream.
KtKotlin
import com.github.doyaaaaaken.kotlincsv.dsl.csvReader
import java.io.File
fun main() {
csvReader().open(File("data.csv")) {
readAllAsSequence().forEach { row ->
println(row.first())
}
}
}com.github.doyaaaaaken:kotlin-csv-jvm — or reuse any JVM library (opencsv) directly.
RbRuby
require 'csv'
CSV.foreach('data.csv', headers: true) do |row|
puts row['name']
endCSV is in the stdlib. headers: true yields hash-like CSV::Row objects.