0

I want to check for an existing header in a newly created csv-file with the csv.Sniffer(). But I always get the same error:

:_csv.Error: Could not determine delimiter

The Code:

import csv

with open('example.csv', 'w') as csvfile:
    print("file created")

with open('example.csv', 'r') as check_header_file:
            has_header = csv.Sniffer().has_header(check_header_file.read(1024))

I've already tried to increase the size from 1024 to 2048 and to 3072.And I i tried to open the csv in 'rb' mode instead of only 'r'. For those who are interestet in the complete Traceback Call:

Traceback (most recent call last):
  File "c:/Users/USER/Documents/Hobby/Test.py", line 6, in <module>
    has_header = csv.Sniffer().has_header(check_header_file.read(1024))
  File "C:\Users\USER\AppData\Local\Programs\Python\Python38\lib\csv.py", line 393, in has_header
    rdr = reader(StringIO(sample), self.sniff(sample))
  File "C:\Users\USER\AppData\Local\Programs\Python\Python38\lib\csv.py", line 187, in sniff
    raise Error("Could not determine delimiter")
_csv.Error: Could not determine delimiter

And some pictures of the empty .csv where I'm trying to read the headers from

used csv-file in excel

used csv-file in VSCode

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245

1 Answers1

0

You can't check an empty .csv file with the python csv Sniffer. So I Tried a different approach with try and except. My Solution:

import csv

#array of Headers
fields = ['Header1', 'Header2', 'Header3'] 

#if .csv file doesn't exist, create one
#try to read from desired file, to check if one exists
try:
    with open('example.csv', 'r', newline='') as csvfile: 
        print('file exists')
#if desired file doesn't exist, create it with the write ('w') or 
#append 'a' mode
except:
    with open('example.csv', 'a', newline='') as csvfile: 
        print('file created')

#if .csv file doesn't contain Headers, populate it with Headers
#read first row in file, to check if some Headers exists 
with open('example.csv', 'r') as csvfile:
    try:
        reader = csv.reader(csvfile)
        row1 = next(reader)
        print("headers exist")
        print(row1)
#if no Headers exists, create Headers with the help of the 
#previously declared and initialized fields array
    except:
        with open('example.csv', 'w', encoding='UTF8', newline='') as csvfile: 
            writer = csv.DictWriter(csvfile, fieldnames = fields) 
            writer.writeheader()
            print("write headers")

First you check with try if a file already exists or has Headers. And in except will the file or the Header created, if none exists

  • Please provide additional details in your answer. As it's currently written, it's hard to understand your solution. – Community Sep 05 '21 at 19:58
  • Is there a specific reason not to use os module? – OneCricketeer Sep 06 '21 at 12:21
  • I don't really like to use an extra library for a problem that can be solved without it. And i had some problems in the past to check for a file through a specific path – TheOneEmployee Sep 06 '21 at 14:52
  • `os` and `csv` are part of the Standard library, there is no "cost" to import them. Using try-except statements is actually slower than checking directly for file existence or sizes – OneCricketeer Sep 06 '21 at 15:07