8

I want to read 2 different types of CSV-files:

  • one with a ',' as delimiter
  • one with a ';' as delimiter

I tried to check which delimiter I'm using by doing:

dialect = csv.Sniffer().sniff(csvfile, [',', ';'])  
data = csv.reader(csvfile, dialect)

but then I get the TypeError : expected string or buffer.

If I do this, it works, but then I don't know when to use what delimiter.

data = csv.reader(csvfile, delimiter = ",")  
data = csv.reader(csvfile, delimiter = ";")

Can someone help me please?

martineau
  • 119,623
  • 25
  • 170
  • 301
tdhulster
  • 1,531
  • 3
  • 18
  • 32

1 Answers1

29

Sniffer expects a sample string, not a file. All you should need to do is:

dialect = csv.Sniffer().sniff(csvfile.readline(), [',',';'])
csvfile.seek(0)  
data = csv.reader(csvfile, dialect)

The seek is important, because you are moving your current position in the file with the readline command, and you need to reset back to the beginning of the file. Otherwise you lose data.

Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
Spencer Rathbun
  • 14,510
  • 6
  • 54
  • 73
  • 1
    What is the point of listing the delimiters when the Sniffer's sole purpose is to determine them without prior knowledge? – Vaidøtas I. Jan 28 '20 at 12:02
  • 3
    @VaidøtasI. The point is to restrict the delimiter it can found, avoiding weird delimiter detection. On one of my tests, the sniffer found "B" to be my delimiter ..which is clearly not my real csv delimiter. – Quentin Feb 02 '21 at 14:56
  • 1
    @Quentin, oh ok – Vaidøtas I. Feb 02 '21 at 16:41