Is there a way by which I can check if a csv file contains a header without importing any modules like the csv or panda module? I keep getting an error when I try to use readlines as the header contains a utf-8 character (a degree symbol).
Asked
Active
Viewed 506 times
1 Answers
0
You know that the header of a csv file is its first line separated by commas.
By opening the file and reading its first line you can obtain the information that you need:
f = open('file.csv', 'r', encoding='utf-8') #open file
headers = f.readline().split(',') #get headers list
f.close() #close file

AmNotSmort
- 24
- 3
-
Yes I do know this, but my header contains a degree symbol and so I am getting an error as it cannot be read. ```Traceback (most recent call last): File "main.py", line 3, in
headers = file.readline().split(',') File "/usr/lib/python3.8/codecs.py", line 322, in decode (result, consumed) = self._buffer_decode(data, self.errors, final) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb0 in position 40: invalid start byte``` – Rachel Apr 04 '21 at 22:15 -
1you have to specify the encoding while opening the file, I modified my answer. Next time specify the error by putting it explicitly in the Question – AmNotSmort Apr 04 '21 at 22:18
-
I am still getting the same error as before even after modifying my code – Rachel Apr 04 '21 at 22:34
-
You have to know which encoding is used to encode your file because it's not utf-8 if it's failing, then change 'utf-8' with your specific encoding. – AmNotSmort Apr 05 '21 at 04:35