Home > Mobile >  Trouble with pandas converting file in the output
Trouble with pandas converting file in the output

Time:12-31

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'

CodePudding user response:

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 ')

CodePudding user response:

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)

CodePudding user response:

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.

  • Related