0

I just downloaded Pandas through VS Code Python and I'm getting this error on my screen.

I've tried moving around the numbers in the CSV file. The code is finding the file of course. I'm currently using 3.10 through Windows 11.

I'm using this code to print out the CSV file:

import time
import os
import pandas

    while True:
        if os.path.exists("the_basics/temps_today.csv"):
            data = pandas.read_csv("the_basics/temps_today.csv")
            print(data.mean())
        else:
            print("Cannot find file. ")
        time.sleep(10)

This is the CSV file:

st1  st2
23.3 22.1
24.0 23.1
22.1 20.2
19.1 16.8

This is error that I'm getting:

Traceback (most recent call last):
  File "c:\Users\18607\Python_Project_1\the_basics\sketch.py", line 7, in <module>
    data = pandas.read_csv("the_basics/temps_today.csv")
  File "C:\Users\18607\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\util\_decorators.py", line 311, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\18607\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\readers.py", line 586, in read_csv
    return _read(filepath_or_buffer, kwds)
  File "C:\Users\18607\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\readers.py", line 482, in _read
    parser = TextFileReader(filepath_or_buffer, **kwds)
  File "C:\Users\18607\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\readers.py", line 811, in __init__
    self._engine = self._make_engine(self.engine)
  File "C:\Users\18607\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\readers.py", line 1040, in _make_engine
    return mapping[engine](self.f, **self.options)  # type: ignore[call-arg]
  File "C:\Users\18607\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\c_parser_wrapper.py", line 69, in __init__     
    self._reader = parsers.TextReader(self.handles.handle, **kwds)
  File "pandas\_libs\parsers.pyx", line 549, in pandas._libs.parsers.TextReader.__cinit__
pandas.errors.EmptyDataError: No columns to parse from file'
  • Does this answer your question? [How to read file with space separated values in pandas](https://stackoverflow.com/questions/19632075/how-to-read-file-with-space-separated-values-in-pandas) – RJ Adriaansen Dec 30 '21 at 17:35
  • Thank you for your suggestion, although I did try what was suggesting in that post. Unfortunately I had no luck also. – Max9990 Dec 30 '21 at 17:50

3 Answers3

0

CSV stands for comma separated values. Notice the "comma". That means that read_csv by default wants commas in your file. You can add sep='\s+' to read_csv to change that:

data = pandas.read_csv("the_basics/temps_today.csv", sep='\s+')
  • Thank you for responding! I tried that, and just separating the numbers with commas. But I still get the same exact error output? – Max9990 Dec 30 '21 at 17:41
  • Hmm...maybe your pandas version is too old? –  Dec 30 '21 at 17:42
  • I downloaded it yesterday, I have pandas 1.3.5. – Max9990 Dec 30 '21 at 17:44
  • This solution works on pandas 1.3.5 based on the csv shown by OP. Could be an encoding problem in the original csv file being loaded. Try 'csv.Sniffer' maybe that could help. – Aly Abdelaziz Dec 30 '21 at 18:30
0

If you are unable to determine the seperator, you can try using csv.Sniffer to try to find that, and then pass it as a seperator in the DataFrame

dialect_rawdata = csv.Sniffer().sniff(open(name_of_file).readline())
processed_df = pd.read_csv(name_of_file, sep=dialect_rawdata.delimiter)
Aly Abdelaziz
  • 292
  • 4
  • 24
0

ANSWER :

I figured it out!

When I clicked on the file and clicked "copy path". It would separate the addresses using "". I pasted using that. I switched over all the "" into "/" and it gave me an output with no errors.

Got my first "AHA" moment. Thank you guys for responding.