Pandas to_csv 不接受 windows 上的路径
Pandas to_csv does not accept path on windows
I 运行 Python 3.9.5 on Windows 10. 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'
none 我试过的选项似乎有效。
根据您发布的 Traceback,Python_functions.py
中的 create_file('test')
将文件名 'C:/Users/Labor/Documents/data/test2021-05-26 18:47:37.csv' 传递给 df.to_csv(filename)
,这是不正确的(您不能使用冒号 ': ' 在文件名中)。
尝试修改 create_file
的定义,使其在调用 to_csv
之前将冒号替换为下划线,如下所示:filename.replace(":", "_")
.
对此不是 100% 确定,但大约 95%...文件名中有 spaces。
test2021-05-26 18:47:37.csv
这可能是您问题的核心所在。它试图读取文件,就好像 space 之后的信息是参数一样,文件名中的“:”也会导致一些问题。
I 运行 Python 3.9.5 on Windows 10. 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'
none 我试过的选项似乎有效。
根据您发布的 Traceback,Python_functions.py
中的 create_file('test')
将文件名 'C:/Users/Labor/Documents/data/test2021-05-26 18:47:37.csv' 传递给 df.to_csv(filename)
,这是不正确的(您不能使用冒号 ': ' 在文件名中)。
尝试修改 create_file
的定义,使其在调用 to_csv
之前将冒号替换为下划线,如下所示:filename.replace(":", "_")
.
对此不是 100% 确定,但大约 95%...文件名中有 spaces。
test2021-05-26 18:47:37.csv
这可能是您问题的核心所在。它试图读取文件,就好像 space 之后的信息是参数一样,文件名中的“:”也会导致一些问题。