Skip to content

Instantly share code, notes, and snippets.

@gebi
Last active December 19, 2015 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gebi/5985047 to your computer and use it in GitHub Desktop.
Save gebi/5985047 to your computer and use it in GitHub Desktop.
csv test progs in various languages
package main
import (
"encoding/csv"
"flag"
"fmt"
"io"
"os"
)
func main() {
conf_lazyquots := flag.Bool("lazy_quotes", false, "Parse csv with lazy quote rules")
conf_trim_leading_spaces := flag.Bool("trim_leading_space", false, "Trim leading spaces")
conf_columns := flag.Int("columns", -1, "Set number of columns (-1 disables all checks")
flag.Parse()
p := csv.NewReader(os.Stdin)
p.LazyQuotes = *conf_lazyquots
p.TrimLeadingSpace = *conf_trim_leading_spaces
p.FieldsPerRecord = *conf_columns
lineno := 1
for {
line, err := p.Read()
if err == io.EOF {
break
}
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("%d -", lineno)
for num, elem := range line {
fmt.Printf(" %d:%q", num, elem)
}
fmt.Println()
lineno++
}
}
#!/usr/bin/python
import csv
import sys
spamreader = csv.reader(sys.stdin)
lineno = 1
for row in spamreader:
elemno = 0
print lineno, "-",
for elem in row:
print elemno, elem,
elemno += 1
print
lineno += 1
We can make this file beautiful and searchable if this error is corrected: It looks like row 8 should actually have 2 columns, instead of 1. in line 7.
character,quote
Inigo,"You killed my father
Darth, I am your father"
Buddha,""
Dada,'dodo'
Evil Guy,""";drop table"
"Expert", "Trust me, I'm an expert"
Balmer,"""Developers, Developers"""
Yoda,Do,do not.do not try
Me,"Do not
quote me, please"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment