首页 > 解决方案 > Pandas to_csv 不接受 Windows 上的路径

问题描述

我在 Windows 10 上运行 Python 3.9.5。pd.to_csv不接受我的文件名。我尝试了以下文件名:

path='C:User/Labor/.../data/'+'myfilename.csv'
path='C:User\\Labor\\...\\data\\'+'myfilename.csv'
path=r'C:User\Labor\...\data\'+'myfilename.csv'
path=os.path.join('C:User\\Labor\\...\\data','myfilename.csv')

但是我每次都会遇到以下错误:

Traceback (most recent call last):
  File "C:\Users\Labor\Documents\Jonathan\rgb_spektrometer\Python_functions.py", line 53, in <module>
    append_dataframe(create_file('test'),a,position,wavelength,voltage)
  File "C:\Users\Labor\Documents\Jonathan\rgb_spektrometer\Python_functions.py", line 46, in append_dataframe
    df.to_csv(filename)
  File "C:\Users\Labor\AppData\Local\Programs\Python\Python39-32\lib\site-packages\pandas\core\generic.py", line 3387, in to_csv
    return DataFrameRenderer(formatter).to_csv(
  File "C:\Users\Labor\AppData\Local\Programs\Python\Python39-32\lib\site-packages\pandas\io\formats\format.py", line 1083, in to_csv
    csv_formatter.save()
  File "C:\Users\Labor\AppData\Local\Programs\Python\Python39-32\lib\site-packages\pandas\io\formats\csvs.py", line 228, in save
    with get_handle(
  File "C:\Users\Labor\AppData\Local\Programs\Python\Python39-32\lib\site-packages\pandas\io\common.py", line 558, in get_handle
    ioargs = _get_filepath_or_buffer(
  File "C:\Users\Labor\AppData\Local\Programs\Python\Python39-32\lib\site-packages\pandas\io\common.py", line 333, in _get_filepath_or_buffer
    file_obj = fsspec.open(
  File "C:\Users\Labor\AppData\Local\Programs\Python\Python39-32\lib\site-packages\fsspec\core.py", line 134, in open
    out = self.__enter__()
  File "C:\Users\Labor\AppData\Local\Programs\Python\Python39-32\lib\site-packages\fsspec\core.py", line 102, in __enter__
    f = self.fs.open(self.path, mode=mode)
  File "C:\Users\Labor\AppData\Local\Programs\Python\Python39-32\lib\site-packages\fsspec\spec.py", line 942, in open
    f = self._open(
  File "C:\Users\Labor\AppData\Local\Programs\Python\Python39-32\lib\site-packages\fsspec\implementations\local.py", line 120, in _open
    return LocalFileOpener(path, mode, fs=self, **kwargs)
  File "C:\Users\Labor\AppData\Local\Programs\Python\Python39-32\lib\site-packages\fsspec\implementations\local.py", line 202, in __init__
    self._open()
  File "C:\Users\Labor\AppData\Local\Programs\Python\Python39-32\lib\site-packages\fsspec\implementations\local.py", line 207, in _open
    self.f = open(self.path, mode=self.mode)
OSError: [Errno 22] Invalid argument: 'C:/Users/Labor/Documents/data/test2021-05-26 18:47:37.csv'

我尝试过的所有选项似乎都不起作用。

标签: pythonpandasexport-to-csv

解决方案


对此不是 100% 确定,但大约 95% ......文件名中有空格。

test2021-05-26 18:47:37.csv

这可能是您问题的核心所在。它试图读取文件,就好像空格后面的信息是参数一样,文件名中的“:”也会导致一些问题。


推荐阅读