python - 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'
我尝试过的所有选项似乎都不起作用。
解决方案
对此不是 100% 确定,但大约 95% ......文件名中有空格。
test2021-05-26 18:47:37.csv
这可能是您问题的核心所在。它试图读取文件,就好像空格后面的信息是参数一样,文件名中的“:”也会导致一些问题。
推荐阅读
- r - 美学到底指的是什么,我应该如何调整我的数据集以获得“正确的美学”?
- c# - 将旋转多边形内的内容传输到位图
- javascript - 文件下载后重新加载页面
- python - Werkzeug 中的 BrokenFilesystemWarning
- python - 将多个排序的链表合并为一个
- javascript - 如何使按钮在单击时更改背景和文本颜色
- regex - Notepad++ / BOOST 中的错误或我的正则表达式中的错误?
- ruby-on-rails - Rails Arel_table 通过范围连接多个表
- karate - 响应是 jpg 文件 - 如何在 KARATE 中编写请求?
- javascript - 具有内部 js 和 css 的 Web 组件不显示内容